Share delegate c#

thanhthienngoai

New member
## Đại biểu trong C#

Một đại biểu là một loại đại diện cho một chữ ký phương thức.Nó có thể được sử dụng để chỉ một phương thức theo tên, mà không cần biết các chi tiết thực hiện của phương thức.Các đại biểu được sử dụng để thực hiện các cuộc gọi lại, trình xử lý sự kiện và các tình huống khác, nơi bạn cần gọi một phương thức sau đó hoặc từ một bối cảnh khác.

## Tạo một đại biểu

Để tạo một đại biểu, bạn sử dụng từ khóa đại biểu theo sau tên của loại đại biểu và chữ ký phương thức.Ví dụ: mã sau tạo một đại biểu gọi là `mydelegate` có hai tham số số nguyên và trả về một số nguyên:

`` `C#
Đại biểu công cộng Int MyDelegate (int x, int y);
`` `

## Gọi một đại biểu

Khi bạn đã tạo một đại biểu, bạn có thể gọi nó bằng cách sử dụng phương thức `indoke '.Phương thức `indoke` lấy đại biểu làm tham số đầu tiên của nó, theo sau là các đối số cho chữ ký phương thức của đại biểu.Ví dụ: mã sau đây gọi là đại biểu `mydelegate` với các đối số 10 và 20: 20:

`` `C#
int result = mydelegate.invoke (10, 20);
`` `

## chuyển các đại biểu cho các phương thức

Bạn cũng có thể chuyển các đại biểu cho các phương thức.Khi bạn chuyển một đại biểu cho một phương thức, phương thức sẽ gọi đại biểu khi nó được thực thi.Ví dụ: mã sau đây xác định một phương thức gọi là `dosomothing 'lấy một đại biểu làm tham số của nó.Phương thức sau đó gọi đại biểu với các đối số 10 và 20:

`` `C#
công khai void dosomothing (đại biểu MyDelegate)
{
đại biểu.invoke (10, 20);
}
`` `

Sau đó, bạn có thể gọi phương thức `dosomothing` bằng cách chuyển nó một đại biểu cho một phương thức mà bạn muốn gọi.Ví dụ: mã sau gọi phương thức `dosomothing` với một đại biểu cho phương thức` math.add`:

`` `C#
Dosome (đại biểu (int x, int y) => math.add (x, y));
`` `

## Sử dụng các đại biểu với các sự kiện

Các đại biểu thường được sử dụng với các sự kiện.Một sự kiện là một thông báo được gửi khi một cái gì đó xảy ra trong ứng dụng của bạn.Khi bạn đăng ký một sự kiện, bạn cung cấp một đại biểu sẽ được gọi khi sự kiện được nêu ra.Ví dụ: mã sau đăng ký sự kiện `click` của nút và cung cấp một đại biểu sẽ in tin nhắn vào bảng điều khiển khi nhấp vào nút:

`` `C#
nút.click += đại biểu {
Console.WriteLine ("Bấm nút");
};
`` `

## Phần kết luận

Các đại biểu là một công cụ mạnh mẽ có thể được sử dụng để tách mã và thực hiện các cuộc gọi lại và trình xử lý sự kiện.Bằng cách hiểu cách các đại biểu làm việc, bạn có thể viết mã linh hoạt và có thể duy trì hơn.

## hashtags

* #csharp
* #Delegates
* #Sự kiện
* #Callbacks
* #Decoupling
=======================================
## Delegate in C#

A delegate is a type that represents a method signature. It can be used to refer to a method by name, without needing to know the implementation details of the method. Delegates are used to implement callbacks, event handlers, and other scenarios where you need to call a method at a later time or from a different context.

## Creating a delegate

To create a delegate, you use the delegate keyword followed by the name of the delegate type and the method signature. For example, the following code creates a delegate called `MyDelegate` that takes two integer parameters and returns an integer:

```c#
public delegate int MyDelegate(int x, int y);
```

## Calling a delegate

Once you have created a delegate, you can call it by using the `Invoke` method. The `Invoke` method takes the delegate as its first parameter, followed by the arguments for the delegate's method signature. For example, the following code calls the `MyDelegate` delegate with the arguments 10 and 20:

```c#
int result = MyDelegate.Invoke(10, 20);
```

## Passing delegates to methods

You can also pass delegates to methods. When you pass a delegate to a method, the method will call the delegate when it is executed. For example, the following code defines a method called `DoSomething` that takes a delegate as its parameter. The method then calls the delegate with the arguments 10 and 20:

```c#
public void DoSomething(MyDelegate delegate)
{
delegate.Invoke(10, 20);
}
```

You can then call the `DoSomething` method by passing it a delegate to a method that you want to call. For example, the following code calls the `DoSomething` method with a delegate to the `Math.Add` method:

```c#
DoSomething(delegate(int x, int y) => Math.Add(x, y));
```

## Using delegates with events

Delegates are often used with events. An event is a notification that is sent when something happens in your application. When you subscribe to an event, you provide a delegate that will be called when the event is raised. For example, the following code subscribes to the `Click` event of a button and provides a delegate that will print a message to the console when the button is clicked:

```c#
button.Click += delegate {
Console.WriteLine("Button clicked");
};
```

## Conclusion

Delegates are a powerful tool that can be used to decouple code and to implement callbacks and event handlers. By understanding how delegates work, you can write more flexible and maintainable code.

## Hashtags

* #csharp
* #Delegates
* #events
* #Callbacks
* #Decoupling
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top