A beginners guide to delegates with examples

Delegates- (Namespace: System.Delegate)

**Definition: **A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike function pointers, delegates are object-orientedtype-safe and secure. The type of delegate is defined by the name of the delegate.

(The above definition is from** MSDN**.)

OR

A delegate is a reference type that holds the reference to a method or group of methods

Now for a beginner, some of these terminologies might be strange and one might have several questions in one’s mind :

·Question 1- How does one declare and instantiate a delegate?

·Question 2- How does one associate methods to it?

·Question 3- Where are these delegates used in the real world and have I ever come across them knowingly or unknowingly?

Let us get to the first two questions:

Question 1- How does one declare and instantiate a delegate?

Question 2-How does one associate methods to it?

Syntax : delegate

Declaration:- public delegate int MyDelegate (string s);

MyDelegate” here is a delegate which accepts a string as a parameter and returns an integer. The delegate and the methods it wishes to refer must have identical signatures.

Instantiating a delegate & associating one or more methods to it.

A delegate is instantiated by using the new keyword and associated with a method.

Example: public delegate int NumberTailor(int x, int y);

This is how we instantiate it:

var delegate1 = new NumberTailor(DelegateHelpers.AddNumbers);

var delegate2 = new NumberTailor(DelegateHelpers.MultiplyNumbers);

Image for post

Function and Delegate Signature should be identical.

We then call the methods:

Console.WriteLine(delegate1(10,29));

Console.WriteLine(delegate2(25,2500));

Image for post

Calling of methods via delegate and its output.

As mentioned earlier in the declaration section, a delegate can be used to call any method matching its signature, however, the function must strictly adhere to its signature or else it throws a compilation error (example shown later).

Multicast Delegate

We will now look at an example where we can add references to more than one method to the delegate.

class DelegateHelpers

{
public static string TamperWithEvidence(string evidence)
{
var tamperedEvidence = string.Empty;
if (evidence.ToLower().Contains(“evidence”))
{
tamperedEvidence = evidence.Replace(“evidence”, “***d****”);
}
return tamperedEvidence;
}
public static string DoNotTamperWithEvidence(string evidence)
{
return string.Empty;
}
}

The code above is of the two methods we shall refer through our delegate. Notice here, **DoNotTamperWithEvidence(string) **method always returns an empty string, whilst TamperWithEvidence(string) returns a tampered evidence string.

class Program

{
public delegate string StringTailor(string str);
static void Main(string[] args)
{
var stringTailor = new StringTailor(DelegateHelpers.TamperWithEvidence);
//stringTailor += DelegateHelpers.DoNotTamperWithEvidence;
var oldString = “The was evidence against him.”;
var newString = stringTailor(oldString);
Console.WriteLine(“old string = “ + oldString
+ “\nnew string = “ + newString
);
}
}

The code above is our main method wherein we call our methods via the delegates. A thing to notice here is that we have commented out the line where we add a new method reference to our delegate. Let us look at the output of the same.

Image for post

#coding-interviews #programming #dotnet #c-sharp-programming #interview

Delegates in C#- Part 1
1.40 GEEK