Delegates
A
delegate in C# is similar to a function pointer in C or C++. Using a delegate
allows the programmer to encapsulate a reference to a method inside a delegate
object. The delegate object can then be passed to code which can call the
referenced method, without having to know at compile time which method will be
invoked. Unlike function pointers in C or C++, delegates are object-oriented,
type-safe, and secure.
A
delegate declaration defines a type that encapsulates a method with a
particular set of arguments and return type
DELEGATES
1.
DELEGATE IS AN FUNCTION POINTER
2.
IT PASSES AN REFRENCES
3.
EVENTS FIRES THROUGH THE DELEGATES
4.
YOU CAN HANDLE THE ENVENT
5.
DELEGATES IS AN CONCEPT TYPE WE CALL THE FUNCTION
AS AN REFRENCE
6.
WITH THE HELP OF MULTITASK DELEGATES WE USE
MULTIPLE FUNCTIONS IN AN EVENTS ON A SINGLE BUTTON
7.
BUT IN MULTIUTASK DELEGATE RETURN TYPE SHOULD BE
VOID TYPE
8.
THROUGHT THE DELEGATES WE CAN HIDE THE FUNCTION
NAME AT THE CALLING TIME
9.
AND IN MULTITASK DELEGATES ARE EVENTS SHOULD BE
VOID TYPE
Parameters should
be same otherwise it will create error and return type should be same
SINGLE TASK DELEGATES
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
delegates
{
public delegate int abc(int a, int b);
class Class1
{
public abc stay;
public Class1()
{
stay =new abc(sum);
}
private int sum(int a, int b)
{
int
c = a + b;
return c;
}
}
}
ON BUTTON TO CALL
private void button1_Click(object
sender, EventArgs e)
{
Class1 ob = new Class1();
MessageBox.Show(ob.stay(54, 21).ToString());
}
MULTI TASK DELEGATES
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
delagatesfunctoip
{
public delegate void mdel();
class Class1
{
public mdel kopal;
public Class1()
{
kopal = new mdel(hello);
kopal += new mdel(good);
kopal += new mdel(welcome);
}
private void hello()
{
int a = 23;
int b = 98;
int c = a + b;
MessageBox.Show(c.ToString());
}
private void good()
{
int a = 98;
int b = 23;
int c = a - b;
MessageBox.Show(c.ToString());
}
private void
welcome()
{
MessageBox.Show("welcome");
}
}
}
private void multitask_Click(object
sender, EventArgs e)
{
Class1 ob = new Class1();
ob.stay();
}
private void
button2_Click(object sender, EventArgs e)
{
Class2 ob = new Class2();
MessageBox.Show(ob.show(12,3).ToString());
}
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
WindowsFormsApplication11
{
public delegate void iamclicked();
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public event iamclicked
clickonme;
private
void Form1_Load(object
sender, EventArgs e)
{
this.clickonme
+=new iamclicked(Form1_clickonme);
}
void
Form1_clickonme()
{
MessageBox.Show("welcome");
}
private
void button1_Click(object
sender, EventArgs e)
{
clickonme();
}
}
}
RUNTIME EVENTS EVENTS
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace
WindowsFormsApplication9
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
Button
btn;
TextBox
t1, t2, t3;
private
void Form1_Load(object
sender, EventArgs e)
{
t1 = new
TextBox();
t1.Location = new Point(120,
50);
this.Controls.Add(t1);
t2 = new
TextBox();
t2.Location = new Point(120,
80);
this.Controls.Add(t2);
t3 = new
TextBox();
t3.Location = new Point(120,
130);
this.Controls.Add(t3);
btn = new
Button();
btn.Name = "b1";
btn.Text = "submit";
btn.Location = new Point(120,
180);
btn.Click += new EventHandler(btn_Click);
btn.MouseHover += new EventHandler(btn_MouseHover);
this.Controls.Add(btn);
}
void
btn_MouseHover(object sender, EventArgs e)
{
MessageBox.Show("please click on me");
}
void
btn_Click(object sender, EventArgs e)
{
int
a = Convert.ToInt32(t1.Text);
int
b = Convert.ToInt32(t2.Text);
int
c = a + b;
t3.Text = c.ToString();
}
}
}
No comments:
Post a Comment