Click here to Skip to main content
Licence 
First Posted 22 Jun 2006
Views 9,311
Bookmarked 8 times

Your first delegate is 4 steps away

By | 22 Jun 2006 | Article
Writing your first delegate is as simeple as that of eating a pice of cake

Introduction

When I first hear about delegates and events I searched in internet to get an easily understandable article or tutorial , but unfortunately most of the examples and tutorials taken me to the dark areas of confusion and finally I landed up in no mans land. This is the sole purpose of writing a most simplified article about delegates and events.

 

What is Delegate?

 

Delegate is nothing but function pointer in C++.(Function Pointers are pointers, i.e. variables, which point to the address of a function. You must keep in mind, both executable compiled code and variables gets a certain space in memory. Instead of calling a method by its we are calling the method with the address location of it in the memory)

 

Let's go to write your First Delegate

 

 

using System;

 

namespace DElegate

{

      /// <summary>

      /// Summary description for Hello.

      /// </summary>

      class Hello

      {          

            //Step 1. Declaring a delegate which can carry address of  anyfunction who dont take any parameters

            public delegate void MyHelloDelegate();

 

            //Step 2.A normal class member function

            public void SayHello()

            {

                  Console.WriteLine("Say hello to the world through delegate");

            }

 

            /// <summary>

            /// Main Entry point

            /// </summary>

            [STAThread]

            static void Main(string[] args)

            {

                  //Creating a class object to call any methods of your class

                  Hello classObject=new Hello();

              //Step 3.Creating a delegate object and pass the address of the function you want to call

                  //through the delegate

                  MyHelloDelegate helloDelegateObj=new MyHelloDelegate(classObject.SayHello);

                  //Step 4. Call the function through delegate.Ie through function pointer

                  helloDelegateObj();

                  //Belive me you are done

                  Console.ReadLine();

                 

            }

      }

}

 

Believe me you are done with your delegate. In the above code if you add a few more code if you want to pass any parameter in delegate.

//Step1

 public delegate void MyHelloDelegate1(string s);

//Step2

public void SayHello1(string name)

            {

                  Console.WriteLine("{0} Say hello to the world through delegate",name);

            }

//Step3

MyHelloDelegate1 helloDelegateObj1=new MyHelloDelegate1(classObject.SayHello1);

 

//Step4

helloDelegateObj1("arun");

 

 

That’s all about delegates. Remember delegates always use in conjunction with Events. We have to only worry that the function signature is matching with your delegate or not. Moreover delegates are not part of any class, you can use the same delegate for any class of your concern but the only mandatory thing is your function signature matches with delegate declaration.

 

 

 

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Arun K Jose

Web Developer

India India

Member

Hei this is Arun Jose. I always like to be a good student, always intrested to learn and understand Microsoft Technologies. Working in .net and BizTalk for the last two years. My hobbies are reading(oh!!! not novels only technical, in my life I never read a novel). Currenly working in a company in Bangalore.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThere are legitimate reasons PinmemberEnnis Ray Lynch, Jr.4:18 23 Jun '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 23 Jun 2006
Article Copyright 2006 by Arun K Jose
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid