0% found this document useful (0 votes)
17 views

Delegateprogram

1. A delegate is a reference type that defines a signature for methods. 2. A delegate variable can refer to any method that matches the delegate's signature. 3. Delegates allow methods to be passed as parameters and invoked like normal functions.

Uploaded by

drakefeel408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Delegateprogram

1. A delegate is a reference type that defines a signature for methods. 2. A delegate variable can refer to any method that matches the delegate's signature. 3. Delegates allow methods to be passed as parameters and invoked like normal functions.

Uploaded by

drakefeel408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Points to Remember :

1. Delegate is the reference type data type that defines


the signature.
2. Delegate type variable can refer to any method with
the same signature as the delegate.
3. Syntax: [access modifier] delegate [return type]
[delegate name]([parameters])
4. A target method's signature must match with delegate
signature.
5. Delegates can be invoke like a normal function or
Invoke() method.
6. Multiple methods can be assigned to the delegate
using "+" or "+=" operator and removed using "-" or
"-=" operator. It is called multicast delegate.
7. If a multicast delegate returns a value then it returns
the value from the last assigned target method.
8. Delegate is used to declare an event and anonymous
methods in C#.
public delegate void MyDelegate(string msg); //declaring a
delegate

class Program
{
static void Main(string[] args)
{
MyDelegate del = ClassA.MethodA;
del("Hello World");

del = ClassB.MethodB;
del("Hello World");

del = (string msg) => Console.WriteLine("Called lambda


expression: " + msg);
del("Hello World");
}
}

class ClassA
{
static void MethodA(string message)
{
Console.WriteLine("Called ClassA.MethodA() with
parameter: " + message);
}
}

class ClassB
{
static void MethodB(string message)
{
Console.WriteLine("Called ClassB.MethodB() with
parameter: " + message);
}
}
Passing Delegate as a Parameter
public delegate void MyDelegate(string msg); //declaring a
delegate

class Program
{
static void Main(string[] args)
{
MyDelegate del = ClassA.MethodA;
InvokeDelegate(del);

del = ClassB.MethodB;
InvokeDelegate(del);

del = (string msg) => Console.WriteLine("Called lambda


expression: " + msg);
InvokeDelegate(del);
}

static void InvokeDelegate(MyDelegate del) // MyDelegate type


parameter
{
del("Hello World");
}
}

class ClassA
{
static void MethodA(string message)
{
Console.WriteLine("Called ClassA.MethodA() with
parameter: " + message);
}
}

class ClassB
{
static void MethodB(string message)
{
Console.WriteLine("Called ClassB.MethodB() with
parameter: " + message);
}
}
Multicast Delegate

public delegate void MyDelegate(string msg); //declaring a


delegate

class Program
{
static void Main(string[] args)
{
MyDelegate del1 = ClassA.MethodA;
MyDelegate del2 = ClassB.MethodB;

MyDelegate del = del1 + del2; // combines del1 + del2


del("Hello World");

MyDelegate del3 = (string msg) =>


Console.WriteLine("Called lambda expression: " + msg);
del += del3; // combines del1 + del2 + del3
del("Hello World");

del = del - del2; // removes del2


del("Hello World");

del -= del1 // removes del1


del("Hello World");
}
}

class ClassA
{
static void MethodA(string message)
{
Console.WriteLine("Called ClassA.MethodA() with
parameter: " + message);
}
}

class ClassB
{
static void MethodB(string message)
{
Console.WriteLine("Called ClassB.MethodB() with
parameter: " + message);
}
}
Multicast Delegate Returning a Value

public delegate int MyDelegate(); //declaring a delegate

class Program
{
static void Main(string[] args)
{
MyDelegate del1 = ClassA.MethodA;
MyDelegate del2 = ClassB.MethodB;

MyDelegate del = del1 + del2;


Console.WriteLine(del());// returns 200
}
}

class ClassA
{
static int MethodA()
{
return 100;
}
}

class ClassB
{
static int MethodB()
{
return 200;
}
}
A generic delegate can be defined the same way as a delegate but using generic
type parameters or return type. The generic type must be specified when you set
a target method.
public delegate T add<T>(T param1, T param2); // generic delegate

class Program
{
static void Main(string[] args)
{
add<int> sum = Sum;
Console.WriteLine(sum(10, 20));

add<string> con = Concat;


Console.WriteLine(conct("Hello ","World!!"));
}

public static int Sum(int val1, int val2)


{
return val1 + val2;
}

public static string Concat(string str1, string str2)


{
return str1 + str2;
}
}

You might also like