Delegate C# Là Gì

What if we want khổng lồ pass a function as a parameter? How does C# handles the callbaông xã functions or sự kiện handler? The answer is - delegate.

The delegate is a reference type data type that defines the method signature. You can define variables of delegate, just like other data type, that can refer to any method with the same signature as the delegate.

There are three steps involved while working with delegates:

Declare a delegate Set a target method Invoke a delegate

A delegate can be declared using the delegate keywords followed by a function signature, as shown below.


Bạn đang xem: Delegate c# là gì

Above, we have sầu declared a delegate MyDelegate with a void return type & a string parameter. A delegate can be declared outside of the class or inside the class. Practically, it should be declared out of the class.

After declaring a delegate, we need lớn mix the target method or a lambda expression. We can vì chưng it by creating an object of the delegate using the new từ khóa và passing a method whose signature matches the delegate signature.


public delegate void MyDelegate(string msg); // declare a delegate// phối target methodMyDelegate del = new MyDelegate(MethodA);// or MyDelegate del = MethodA; // or set lambdomain authority expression MyDelegate del = (string msg) => Console.WriteLine(msg);// target methodstatic void MethodA(string message) Console.WriteLine(message);
You can mix the target method by assigning a method directly without creating an object of delegate e.g., MyDelegate del = MethodA.

After setting a target method, a delegate can be invoked using the Invoke() method or using the () operator.


public delegate void MyDelegate(string msg); //declaring a delegateclass 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 lambdomain authority 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);
The following image illustrates the delegate.

*
C# Delegate

Passing Delegate as a Parameter

A method can have a parameter of the delegate type, as shown below.


public delegate void MyDelegate(string msg); //declaring a delegateclass Program static void Main(string<> args) MyDelegate del = ClassA.MethodA; InvokeDelegate(del); del = ClassB.MethodB; InvokeDelegate(del); del = (string msg) => Console.WriteLine("Called lambdomain authority 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);
In .NET, Func và Action types are built-in generic delegates that should be used for most comtháng delegates instead of creating new custom delegates.

Xem thêm: Trang Trí Lối Đi Sân Vườn Nhỏ Hẹp, 5 Mẫu Thiết Kế Lối Đi Sân Vườn Đẹp Quên Lối Về

Multicast Delegate

The delegate can point khổng lồ multiple methods. A delegate that points multiple methods is called a multicast delegate. The "+" or "+=" operator adds a function khổng lồ the invocation các mục, và the "-" & "-=" operator removes it.


public delegate void MyDelegate(string msg); //declaring a delegateclass 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 lambdomain authority 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);
The addition and subtraction operators always work as part of the assignment: del1 += del2; is exactly equivalent to lớn del1 = del1+del2; và likewise for subtraction.

If a delegate returns a value, then the last assigned target method"s value will be return when a multicast delegate called.


public delegate int MyDelegate(); //declaring a delegateclass 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;

Generic Delegate

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 mix a target method.

For example, consider the following generic delegate that is used for int và string parameters.


public delegate T add(T param1, T param2); // generic delegateclass Program static void Main(string<> args) addint> sum = Sum; Console.WriteLine(sum(10, 20)); addstring> 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;

*
Points khổng lồ Rethành viên :