0

A Delegate in my perspective seems to be one of the challenging concepts to learn.

From my understanding, delegate is a method pointer which points to a particular method at runtime.

One of the examples I was for delegates was during file handling where some file operation can be performed before calling a method and releasing the file resources after the method call. Using delegate here improves reusability.

My question is, can you enlighten me on other practical uses of delegates on day to day programming?

Thank is advance!

Karthik
  • 187
  • 3
  • 11

4 Answers4

3

Well, by and large the most predominant use of delegates is via events and their handlers. I can't tell if you realize it yet due to the way you have worded your question, but every time you write

someObj.SomeEvent += SomeMethod;

You are using delegates, specifically, SomeMethod is being wrapped by a delegate instance.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
0

Check out http://csharpindepth.com/Articles/Chapter2/Events.aspx

It is way too much to type here

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
0

You will also use delegate with Threading :

//Anynymous delegate
new Thread(delegate() 
{
      Console.WriteLine("Hello world form Thread");
});

//Lambda expression
new Thread(() => 
{
      Console.WriteLine("Hello world form Thread");
});

Have a look to lambda expression, it is powerful: Lambda expression

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
Steven Muhr
  • 3,339
  • 28
  • 46
  • Thanks to you, my awareness about using threads with delegates has increased as I was not aware of the same. I am looking forward to your posts. – Karthik Nov 09 '11 at 09:52
0

Delegates are useful when used with custom event handlers.

Delegates are the rules that you can define for your calling method at run time.

E.g public delegate void NameIndicator( string name );

You can bind a method to a delegate and register it to an event.

Please see the example below.

public delegate void NameIndicator( string name );

class Program
{
    static void Main( string[] args )
    {
        //Create the instance of the class
        Car car = new Car( "Audi" );
        //Register the event with the corresponding method using the delegate
        car.Name += new NameIndicator( Name );
        //Call the start to invoke the Name method below at runtime.
        car.Start();
        Console.Read();
    }

    /// <summary>
    /// The method that can subscribe the event of the defined class.
    /// </summary>
    /// <param name="name">Name assigned from the caller.</param>
    private static void Name( string name )
    {
        Console.WriteLine( name );
    }

}

public class Car
{
    //Event for the car class.
    public event NameIndicator Name;
    string name;

    public Car( string nameParam )
    {
        name = nameParam;
    }

    //Invoke the event when the start method is called.
    public virtual void Start()
    {
        Name( name );
    }
}
Karthik
  • 187
  • 3
  • 11