Share c# delegate

nguyentuan.long

New member
#C ##Delegate #FoctionPuler #Callback #Event ## C #Ủy quyền

Một đại biểu là một loại đại diện cho một phương thức có thể được gọi sau này.Nó tương tự như một con trỏ hàm trong C ++, nhưng nó có một số tính năng bổ sung.

Các đại biểu được sử dụng để tách mã gọi một phương thức từ mã thực hiện phương thức.Điều này có thể làm cho mã của bạn mô -đun hơn và dễ bảo trì hơn.

Để tạo một đại biểu, bạn sử dụng từ khóa đại biểu.Mã sau tạo một đại biểu đại diện cho một phương thức với một tham số INT và không có giá trị trả về:

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

Sau đó, bạn có thể gán một phương thức cho một đại biểu bằng toán tử = toán tử.Ví dụ: mã sau đây gán phương thức myMethod cho đại biểu MyDelegate:

`` `C#
mydelegate = mymethod;
`` `

Bạn có thể gọi một đại biểu bằng cách sử dụng phương thức gọi.Mã sau đây gọi đại biểu MyDelegate với giá trị 10:

`` `C#
mydelegate.invoke (10);
`` `

Các đại biểu thường được sử dụng để xử lý các sự kiện.Khi một sự kiện xảy ra, trình xử lý sự kiện được gọi bằng cách gọi các đại biểu được liên kết với sự kiện.

Để biết thêm thông tin về các đại biểu, hãy xem [Tài liệu C#] (Delegates - C# Programming Guide - C#).

## hashtags

* #c#
* #Delegate
* #FoctionPulum
* #gọi lại
* #Sự kiện
=======================================
#c# #Delegate #functionpointer #Callback #Event ## C# Delegate

A delegate is a type that represents a method that can be called later. It is similar to a function pointer in C++, but it has some additional features.

Delegates are used to decouple the code that calls a method from the code that implements the method. This can make your code more modular and easier to maintain.

To create a delegate, you use the delegate keyword. The following code creates a delegate that represents a method with one int parameter and no return value:

```c#
public delegate void MyDelegate(int x);
```

You can then assign a method to a delegate using the = operator. For example, the following code assigns the MyMethod method to the myDelegate delegate:

```c#
myDelegate = MyMethod;
```

You can call a delegate by using the Invoke method. The following code calls the myDelegate delegate with the value 10:

```c#
myDelegate.Invoke(10);
```

Delegates are often used to handle events. When an event occurs, the event handler is called by invoking the delegate that is associated with the event.

For more information on delegates, see the [C# documentation](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/).

## Hashtags

* #c#
* #Delegate
* #functionpointer
* #Callback
* #Event
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top