C# Advanced (Series #7)

1 min read

C#
  • C# – Anonymous Types
  • C# – Dynamic Types
  • C# – Ternary Operator ?:
  • C# Generics & Generic Constraints
  • C# – Delegates
  • C# – Func Delegate
  • C# – Action Delegate
  • C# – Anonymous Method
  • C# – Events
  • C# – Extension Method
  • C# – HttpClient

C# – Action Delegate

Action là một loại delegate được xác định trong System namespace. Action type delegate giống như Func delegate ngoại trừ việc Action delegate không trả về giá trị. Nói cách khác, bạn có thể sử dụng Action delegate với một phương thức có loại trả về void.

Ví dụ: delegate sau in ra một giá trị int.

// Example: C# Delegate
public delegate void Print(int val);
static void ConsolePrint(int i)
{
    Console.WriteLine(i);
}
static void Main(string[] args)
{           
    Print prnt = ConsolePrint;
    prnt(10);
}
// OUTPUT: 10

Bạn có thể sử dụng Action delegate thay vì xác định Print delegate ở trên, ví dụ:

// Example: Action delegate
static void ConsolePrint(int i)
{
    Console.WriteLine(i);
}
static void Main(string[] args)
{
    Action<int> printActionDel = ConsolePrint;
    printActionDel(10);
}
// OUTPUT: 10

Bạn có thể khởi tạo một Action delegate bằng từ khóa new hoặc bằng cách gán trực tiếp một phương thức:

Action<int> printActionDel = ConsolePrint;
//Or
Action<int> printActionDel = new Action<int>(ConsolePrint);

An Action delegate can take up to 16 input parameters of different types.

An Anonymous method can also be assigned to an Action delegate, for example:

// Example: Anonymous method with Action delegate
static void Main(string[] args)
{
    Action<int> printActionDel = delegate(int i)
                                {
                                    Console.WriteLine(i);
                                };
    printActionDel(10);
}
// OUTPUT: 10

Một biểu thức Lambda cũng có thể được sử dụng với Action delegate:

// Example: Lambda expression with Action delegate
static void Main(string[] args)
{
    Action<int> printActionDel = delegate(int i)
                                {
                                    Console.WriteLine(i);
                                };
    printActionDel(10);
}
// OUTPUT: 10

Do đó, bạn có thể sử dụng bất kỳ phương thức nào không trả về giá trị với các loại Action delegate.

Ưu điểm của Action and Func Delegates

  • Dễ dàng và nhanh chóng để xác định các delegates.
  • Làm cho mã ngắn.
  • Khả năng tương thích type trong toàn bộ ứng dụng

Những điểm cần nhớ:

  • Action delegate giống như func delegate ngoại trừ việc nó không trả về bất cứ thứ gì. Kiểu trả về phải trống.
  • Action delegate có thể có 0 đến 16 tham số đầu vào
  • Bạn có thể sử dụng Action delegate với anonymous methods hoặc lambda expressions.
Avatar photo

𝐌𝐄𝐙𝐎𝐍 – 𝐓𝐇𝐄 𝐔𝐋𝐓𝐈𝐌𝐀𝐓𝐄 𝐏𝐋𝐀𝐓𝐅𝐎𝐑𝐌 𝐅𝐎𝐑 𝐂𝐎𝐌𝐌𝐔𝐍𝐈𝐓𝐈𝐄𝐒 & 𝐁𝐔𝐒𝐈𝐍𝐄𝐒𝐒

Discord is a widely used communication platform, but is it truly the best solution for community and business management? While it excels in creating...
Avatar photo Ngan Ton Thuy
3 min read

𝐌𝐞𝐳𝐨𝐧 𝐯𝐬 𝐒𝐥𝐚𝐜𝐤 – 𝐖𝐡𝐢𝐜𝐡 𝐎𝐧𝐞 𝐄𝐧𝐡𝐚𝐧𝐜𝐞𝐬 𝐘𝐨𝐮𝐫 𝐁𝐮𝐬𝐢𝐧𝐞𝐬𝐬…

Both Mezon and Slack are powerful communication platforms, but which one is the right fit for your organization? Let’s compare their key features: 𝐔𝐬𝐚𝐠𝐞...
Avatar photo Ngan Ton Thuy
1 min read

𝐌𝐄𝐙𝐎𝐍 𝐂𝐀𝐌𝐏𝐔𝐒 𝐏𝐀𝐑𝐓𝐍𝐄𝐑 — 𝐄𝐦𝐩𝐨𝐰𝐞𝐫𝐢𝐧𝐠 𝐒𝐭𝐮𝐝𝐞𝐧𝐭-𝐋𝐞𝐝 𝐈𝐧𝐢𝐭𝐢𝐚𝐭𝐢𝐯

Mezon Campus Partner is an all-in-one program designed to support: – Student competitions – Career development initiatives – Student-led clubs and creative teams 𝐖𝐡𝐚𝐭...
Avatar photo Ngan Ton Thuy
44 sec read

Leave a Reply

Your email address will not be published. Required fields are marked *