What are Delegates? How do you use them?

  • A Delegate is a type which can help passing a function as a parameter - similar to pointer functions.
  • These are derived from the Delegate class in .NET and are sealed types.
  • The type of the delegate is determined by the name of the delegate.
  • Methods passed into the delegate type must have the same signature as the defined delegate type.
  • There are three steps:
    1. Declare a delegate type
    2. Instigate the type by associating with a method definition
    3. Invoke it
  • Since the instantiated delegate is an object, it can be passed as a parameter, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time. This is known as an asynchronous callback, and is a common method of notifying a caller when a long process has completed.
void StartingPoint() 
{
     CallDelegate("HipHop", d1);
     CallDelegate("HipHop", d2);
}

// call the passed Del reference 
// with the passed parameter
void CallDelegate(string str, Del d)
{
    // Step 3: Calling the delegate
    d(str);
}

// Step1: declare a delegate type Del 
delegate void Del(string str);

// Step 2: Instigation
Del d1 = delegate (string str)
{
    Console.WriteLine($"Hello {str}");
};

// Step 2: Instigation
Del d2 = str =>
{
    Console.WriteLine($"Hello there! {str} via a Lambda Expression.");
};

Output:
Hello HipHop
Hello there! HipHop via a Lambda Expression.
  • A Delegate is a type which can help passing a function as a parameter – similar to pointer functions.
  • These are derived from the Delegate class in .NET and are sealed types.
  • The type of the delegate is determined by the name of the delegate.
  • Methods passed into the delegate type must have the same signature as the defined delegate type.
  • There are three steps:
    1. Declare a delegate type
    2. Instigate the type by associating with a method definition
    3. Invoke it
  • Since the instantiated delegate is an object, it can be passed as a parameter, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time. This is known as an asynchronous callback, and is a common method of notifying a caller when a long process has completed.
void StartingPoint() 
{
     CallDelegate("HipHop", d1);
     CallDelegate("HipHop", d2);
}

// call the passed Del reference 
// with the passed parameter
void CallDelegate(string str, Del d)
{
    // Step 3: Calling the delegate
    d(str);
}

// Step1: declare a delegate type Del 
delegate void Del(string str);

// Step 2: Instigation
Del d1 = delegate (string str)
{
    Console.WriteLine($"Hello {str}");
};

// Step 2: Instigation
Del d2 = str =>
{
    Console.WriteLine($"Hello there! {str} via a Lambda Expression.");
};

Output:
Hello HipHop
Hello there! HipHop via a Lambda Expression.
Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Privacy Overview
Referbruv

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

3rd Party Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.