Docstoc

Delegate (PDF)

Document Sample
Delegate (PDF) Powered By Docstoc
					Delegate

-----------------

Reference Type



delegate keyword



System.Delegate class



A delegate is used as a 'Function Pointer'.



A delegate can call any method, whose 'return-type'

as well as 'Input signatures' are same as that of the delegate.



Delegates are widely used for 'Event Handling'

in ASP.NET applications.



public delegate void DelA();

public delegate int DelB(int a);

public delegate int DelAdd(int a, int b);

public delegate string DelStr(string x, string y);



---

'+=' is delegate operator

-----



string x = "125";

string y = "4";
MyMath mm = new MyMath();



DelStr d1 = new DelStr(mm.Add); // old way

or

DelStr d1 = mm.Add;      // v3.0 onwards



string result += d1(x, y);// "129"



---

DelStr d2 = mm.Multi;

string resultMulti += d2(x, y); // "500"



===

System Namespace

        - EventHandler delegate



public delegate void EventHandler(object o, EventArgs e);



In Page_Load; // NOT in - if( ! Page.IsPostBack)



BtnSubmit.Click += new EventHandler(this.BtnSubmit_Click);

or

BtnSubmit.Click += this.BtnSubmit_Click;



----
Multi-Casting of delegates

---------------------------------



public delegate void DelA();

public delegate void DelB(int a);

public delegate void DelC(int a, string x);

public delegate void DelD(string x, string y);



DelA d1 = ClassA.MethodA;

d1();



DelB d2 = ClassB.MethodB;

d2(125);



DelC d3 = ClassC.MethodC;

d3(55, "123");



DelD d4 = ClassD.MethodD;

d4("A11", "32");



delegate d = d1 + d2 + d3 + d4; // Multi-Casting



d();

d(125);

d(55, "123");

d("A11", "32")
// Removing a delegate

d -= d1;



//Adding a delegate at run-time

d += d5;



----

Single & Double struct

           - IsNaN property // bool




Operator Overloading

------------------------------

int a = 10;

a += 1; // 11// assignment operator[+=]



string x = "A10-101";

x += 1011;           // "A10-1011011"

                     // Concatenation operator[+=]



string result += d1(x, y);// "129"

BtnSubmit.Click += this.BtnSubmit_Click;

                     // delegate operator[+=]

				
DOCUMENT INFO
Shared By:
Tags: Delegate
Stats:
views:28
posted:2/23/2012
language:
pages:4
Description: Delegate