Sunday, March 13, 2011

Simple steps to understand Delegates in C#

Hi friends, In this article I would like share some important and simple steps about Delegates in C#




Steps:
1) Delegates are similar to C++ Function Pointers
2) Delegates holds the address of one or Address of many functions
3) Delegate Encapsulates (hides) some information like Class names and method names
4) Delegates divided in to 2 types

-->Single cast Delegate
-->Multicast Delegate

Single cast Delegate:
 
Single cast holds the address of one function

Multicast Delegates:

--> Multicast cast delegates hold the address of many functions
--> Multicast cast delegates a sequence of methods
--> Multicast cast delegates supports automatic + & - operators
--> + Operator adds a function into the sequence
--> - Operator removes a function from the sequence

Simple steps for creating delegates: 


Steps 1) write a class with method

Syntax:
               Class Test
               {
                     Public void print ()
                }
Step2) Create a Delegate

Syntax:

               Public delegate void DName ( );

Step 3) create a object to the delegate with the address of a function


Syntax:
Test t=new Test ( );
DName d = new DName (t.print);

Step 4) call the delegate/Raise the Delegate object

Syntax:
                   d ( );


That's it...Happy coding


developercode
About the Author
Sayyad is a Software Engineer, Blogger and Founder of Developers Code from India.His articles mainly focus on .Net (Web and Windows based applications), Mobile Technologies (Android,IPhone,BlackBerry) and SEO.

Labels: , , , ,

1 Comments:

At March 13, 2011 at 6:34 PM , Blogger Dotnet Tutorials said...

Good to share this...

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home