Delegates in C#
Delegates is a type used to hold a reference of method in an object. Delegates
can also be referred as a pointer to function. Delegates are similar to function
pointer in c#.
Syntax of Declaration of Delegates
Public delegate type_of_delegate delegate_name( );
Example
public
delegate void
d();
Note:
Delegate may contain any number of argument or it may be without any parameter
list.
Syntax of Creating Instance of Delegate
public delegate_name name_of_instance;
Example
public
d obj;
Program
using
System;
namespace del
{
class Program
{
public
delegate int
d(int a);
public static
d obj;
public static
int method(int
i)
{
Console.WriteLine("Creating
Method");
return i;
}
static void
Main(string[] args)
{
int n;
obj = new
d(method);
n = obj(8);
Console.WriteLine("value
called via delegate is "+n);
Console.ReadLine();
}
}
}
Output

Description of above Program
* In the above program The Delegate "d" is declared with int return type having
one parameter.
* After that the delegate instance "obj" is created and then a method is
created.
NOTE: Following Syntax should be followed (if you are declaring a method of int
type having one parameter of type int, then delegate which you are declaring
should be in same format this is the reason it is called type safe reason)
* In main method the method name is passed to delegate instance.
obj =
new d(method);
Multicast Delegate
Multicast Delegate is a Delegate which hold the reference of more than one
method.
NOTE: Multicast Delegate
must contain method that returns void.
Program
using
System;
namespace del
{
class Program
{
public delegate
void d(int
a,int b);
public static
d obj;
public static
void sum(int i,
int i1)
{
int c;
c = i + i1;
Console.WriteLine("Creating
Method 1 and value is "+c);
}
public static
void mul(int i,int
i1)
{
int c;
c = i * i1;
Console.WriteLine("Creating
Method 2 and value is " + c);
}
static void
Main(string[] args)
{
obj = new
d(sum);
obj += new
d(mul);
obj(7, 6);
Console.ReadLine();
}
}
}
Output:

Generic Delegate
In this a delegate can define its own parameter.
Syntax of declaration of Generic Delegate
public
delegate void
d<t>(t a);
Syntax of creation of instance of Generic Delegate
d<int>
obj = new d<int>(m1);
Syntax of calling
obj(7);
Program
using System;
namespace del
{
class Program
{
public delegate
void d<t>(t
a);
public static
void m1(int i)
{
Console.WriteLine("Creating
Method 1 and value is " + i);
}
public static
void m2(string
s)
{
Console.WriteLine("Creating
Method 2 and message is " + s);
}
static void
Main(string[] args)
{
d<int>
obj = new d<int>(m1);
d<string>
obj1 = new d<string>(m2);
obj(7);
obj1("hii");
Console.ReadLine();
}
}
}
Output

Anonymous Method in Delegate
Anonymous methods provide
a technique to pass a code block as a delegate parameter. Anonymous methods are
basically methods without a name, just the body.
Syntax
public
delegate void
d (int a);
d obj = delegate(int
a)
{
Console.WriteLine("value
passed is" + a);
};
Program
using System;
namespace del
{
class Program
{
public delegate
void d (int
a);
static void
Main(string[] args)
{
d obj =
delegate(int a)
{
Console.WriteLine("value
passed is " + a);
};
obj(10);
Console.ReadLine();
}
}
}
Output

Advantage of Delegate
1) Improve Performance of Application.
2) Call function Asynchronously.
3) Encapsulate the Method Call from Caller.
Konstantinos AdamopoulosPosted Dec 20, 2022, 11:39 AM
In your second example you are checking if iPhoneX and OnePlus8 both run on IOS. Of course the former is an IOS phone and the latter is an Android phone, so the answer should correctly be false, i.e. "Not all of them runs on IOS!!!". The problem with your example is that if you reverse the order in which the two method references are added to "areBothRunsApple" delegate variable, then the brogram will return true, i.e. "Both runs on IOS!!!", which of course is wrong. This is because when you have a delegate invocation list holding more than one method references when the delegate is invoked it will invoke all the methods in its invocation list and will return the value of the last method invoked. So if you add the GetOnePlus8 method to the delicate invocation list first, and then the GetIPhoneX method second, that second method will get invoked last and will return true, which is wrong. I hope this helps :-)