Take advantage of delegates to lớn promote flexibility in your designs và implement event-driven programming with ease


A delegate is a type-safe function pointer that can reference a method that has the same signature as that of the delegate. You can take advantage of delegates in C# to implement events và call-baông chồng methods. A multicast delegate is one that can point to lớn one or more methods that have sầu identical signatures.

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

Understanding delegates in C#

In essence, a delegate holds a reference lớn a method và also to lớn the target object on which the method should be called. Delegates in C# are similar to lớn function pointers in C++, but C# delegates are type safe. You can pass methods as parameters khổng lồ a delegate lớn allow the delegate to lớn point lớn the method. Delegates are used khổng lồ define callback methods & implement event handling, and they are declared using the “delegate” từ khoá. You can declare a delegate that can appear on its own or even nested inside a class.

There are three steps in using delegates. These include declaration, instantiation, & invocation. 

The signature of a delegate looks lượt thích this:

delegate result-type identifier ()The following statement shows how you can declare a delegate.

public delegate void MyDelegate(string text);As you can see in the above statement, the delegate name is “MyDelegate,” it has a return type of “void,” & it accepts a string object as an argument. This implies that the delegate MyDelegate can point to lớn a method that has an identical signature. This is just a declaration though — you must instantiate a delegate before you can use it. The statement given next shows how you can instantiate the delegate declared above.

MyDelegate d = new MyDelegate(ShowText);Once you have sầu declared và instantiated the delegate, you can invoke the method that the delegate points lớn easily.

d("Hello World...");Here, d is the delegate instance

You can also invoke the method that the delegate instance points lớn using the Invoke() method on the delegate instance as shown below.

d.Invoke("Hello World...");If you have a method that accepts two numbers và you want to add them and return the sum of the two numbers, you can use a delegate to store the return value of the method as shown in the code snippet given below.

Xem thêm: Cách Làm Xà Phòng Từ Sữa Mẹ : Có Tốt Như Mọi Người Vẫn Nghĩ?

int result = d(12, 15);Here is the complete code listing for your reference.

using System;namespace Delegates public delegate int MyDelegate(int x, int y); class Program static int Sum(int x, int y) return x + y; static void Main() MyDelegate d = new MyDelegate(Sum); int result = d.Invoke(12, 15); Console.WriteLine(result); Console.ReadLine();  

Delegates in action in C#

Here is the complete code listing.

using System;namespace Delegates public delegate void MyDelegate(string text); class Program public static void ShowText(string text) Console.WriteLine(text); static void Main() MyDelegate d = new MyDelegate(ShowText); d("Hello World..."); Console.ReadLine(); lưu ý that you can assign multiple objects to a delegate instance using the + operator, which makes the delegate a multicasting delegate. You can also combine delegate instances using the static method Combine.

The runtime maintains a các mục (also called Invocation list) internally to exexinh đẹp multiple methods. The following code listing shows how you can use the Combine() method to combine multiple delegate instances.

myDelegate d1 = new myDelegate(Method1);myDelegate d2 = new myDelegate(Method2);myDelegate multicastDelegate = (myDelegate)Delegate.Combine(d1, d2);multicastDelegate.Invoke();A delegate that can point to multiple methods is known as a multicast delegate. cảnh báo that delegate instances are immutable. So, when you combine delegates or subtract one delegate instance from the danh sách, a new delegate instance is created khổng lồ represent the updated or new list of the targets or methods khổng lồ be invoked.

Multicast delegates in action in C#

The following code listing illustrates a multicast delegate. chú ý the usage of the delegate instance: Here we have sầu used the += operator to lớn assign the delegate khổng lồ multiple methods that have sầu identical signatures.

Xem thêm: Cách Làm Bảng Câu Hỏi Trên Google Docs Forms, Hiện Câu Hỏi Dựa Trên Câu Trả Lời

using System;namespace Delegates public delegate void MyDelegate(); class Program public static void Method1() Console.WriteLine("Inside Method1..."); public static void Method2() Console.WriteLine("Inside Method2..."); static void Main() MyDelegate d = null; d += Method1; d += Method2; d.Invoke(); Console.ReadLine(); Delegates are ideally suited to implementing sự kiện driven programming. A delegate doesn’t need to know the class of the object to lớn which it refers. All it needs to know is the signature of the method to lớn which it would point. Proper usage of delegates can promote reusability in your code và flexibility in your designs. You can refer lớn Microsoft’s online documentation for more information on delegates.