# Delegates

## 😢 ปัญหา

เวลาที่เราเขียนโค้ด แล้วอยากเก็บข้อมูลต่างๆ เช่น ตัวเลข ตัวอักษร อะไรพวกนี้เราก็สามารถสร้างตัวแปรมาเก็บไว้ได้ง่ายๆใช่ป่ะ ตามโค้ดด้านล่างเลย

```csharp
int a = 123;
string b = "saladpuk";
```

แต่ถ้าเรา**อยากเก็บ Method ไว้ในตัวแปรบ้างล่ะ** เราจะทำยังไง?

{% hint style="info" %}
**ข้อแนะนำ**\
เรื่อง delegate ในปัจจุบันเราแทบไม่ได้ยุงกับมันเท่าไหร่อยู่แล้ว เพราะ C# ได้พัฒนาต่อยอดความสามารถเจ้า delegate ออกมาเยอะละ ดังนั้นปรกติเราจะใช้ตัวอื่นแทนนั่นเอง แต่สาเหตุที่ต้องเขียนเรื่องนี้ก็เพราะ เจ้า delegate นี้คือหัวใจหลักของความสามารถอื่นๆที่เราจะเรียกใช้มันยังไงล่ะ
{% endhint %}

## 😄 วิธีแก้ปัญหา

ในภาษา C# เรามีตัวที่ชื่อว่า **Delegate** เอาไว้แก้ปัญหานี้อยู่ ซึ่งการทำงานของมันอธิบายไปตรงนี้เดี๋ยวจะ งง เปล่าๆ ลองไปดูโค้ดจะเข้าใจง่ายกว่า

สมมุติว่าเรามี method อยู่ตัวนึง ตามโค้ดด้านล่าง

```csharp
static void Saladpuk()
{
    Console.WriteLine("Hello world");
}
```

แล้วเราอยากจะเก็บมันไว้ในตัวแปรซักตัว สิ่งที่เราต้องทำก็คือ สร้าง **delegate** ขึ้นมาก่อน  โดยการสร้างเจ้าตัวนี้ เราจะต้องบอกว่า method ของเรามี **Signature** เป็นแบบไหน ซึ่งจาก โค้ดด้านบน เราจะเห็นว่า Signature ของมันคือ

1. มันมี return type เป็น void
2. มันไม่รับ parameter อะไรเลย

จากที่ว่ามาเราก็จะสร้างเจ้า delegate ขึ้นมาก่อน ตามนี้

```csharp
delegate void YOUR_DELEGATE_NAME();
```

ตรง **YOUR\_DELEGATE\_NAME** คือชื่อของ delegate ที่เราสร้างขึ้นมา จะเป็นชื่ออะไรก็ได้ ในตัวอย่างของใช้ชื่อนี้เลยละกันขี้เกียจแก้ละ

ถัดมาเราก็จะสร้างตัวแปร เพื่อเอามาเก็บเจ้า method Saladpuk() ของเรา ดังนั้นก็จะออกมาราวๆนี้

```csharp
YOUR_DELEGATE_NAME del1 = Saladpuk;
```

สุดท้ายเราก็จะลองเรียกใช้งานมันดู

```csharp
del1();
```

> **ผลลัพท์**\
> Hello world

{% hint style="info" %}
ถ้าตัวตัว method ของเรารับ parameter หรือมีการส่งค่ากลับเป็นอย่างอื่นที่ไม่ใช้ void แล้วล่ะก็ เจ้าตัว delegate ของเราก็จะต้องมี signature ที่ตรงกับมันด้วยนะถึงจะใช้งานได้
{% endhint %}

## 📝 Source Code

```csharp
class Program
{
    delegate void YOUR_DELEGATE_NAME();

    static void Main(string[] args)
    {
        YOUR_DELEGATE_NAME del1 = Saladpuk;
        del1();
    }

    static void Saladpuk()
    {
        Console.WriteLine("Hello world");
    }
}
```

## 🎯 บทสรุป

เจ้าตัวนี้ในปัจจุบันเราไม่ได้ไปยุ่งกับมันเท่าไหร่แล้ว แต่เราต้องเข้าใจมันนิดหน่อย เพราะในเรื่องอื่นๆของ C# ได้ใช้หลักเจ้าตัวนี้เยอะอยู่ เช่นพวก `Action`, `Func`, `Lambda` ในบทถัดไปนั่นเอง


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.saladpuk.com/beginner-1/csharp101/advanced/delegates.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
