# 4.การสร้างตัวแปร

อะเคร๊ หลังจากรู้แล้วว่าการเขียนโค้ดนั้นมันจะมีข้อมูลที่เราต้องทำงานด้วย 4 ชนิดคือ **int**, **double**, **string**, **bool** กันละ ในรอบนี้เราจะลองสั่งให้คอมพิวเตอร์ช่วยจำข้อมูลแต่ละประเภทดูหน่อยละกันนะ

{% embed url="<https://www.youtube.com/watch?v=XOTlbsCi9sQ&list=PLUjAn8nwWnijERZ3HpzBk7NfSrau74_lQ&index=7>" %}

{% hint style="info" %}
เวลาที่เราให้คอมมันช่วยจำอะไรสักอย่าง เราจะเรียกสิ่งที่มันจำนี้ว่า **ตัวแปร** หรือ **variable** นั่นเอง

เช่น **int money = 3;** หมายถึงเราให้คอมจำว่าตัวแปรที่ชื่อว่า money มันมีค่าเป็น 3 และมันจำได้เฉพาะตัวเลขจำนวนเต็มเท่านั้น
{% endhint %}

## 🎯 สรุปสั้นๆ

### 👨‍🚀 การสร้างตัวแปรให้คอมช่วยจำ

ชนิดตัวแปร ชื่อตัวแปร;

### 👨‍🚀 กฎในการสร้างตัวแปร

* ชื่อตัวแปรห้ามซ้ำกัน แม้จะเป็นคนละชนิดกันก็ตาม
* ชื่อตัวแปรตัวเล็กตัวใหญ่มีความหมาย (**case sensitive**) ดังนั้น a กับ A ถือว่าเป็นตัวแปรคนละตัวกัน
* ห้ามตั้งชื่อแล้วเว้นวรรค (ถ้าจะทำให้ใช้ตัว **\_** แทน)
* ห้ามตั้งชื่อขึ้นต้นด้วยตัวเลข หรือ **ตัวอักษรพิเศษ** (ยกเว้นตัว **@** และ **\_** สามารถใช้เป็นตัวต้นได้)
* ห้ามตั้งชื่อซ้ำกับ**คำสงวน**ของภาษา C#

{% hint style="danger" %}
สามารถตั้งชื่อภาษาไทยหรือเลขไทยได้นะ แต่แนะนำว่า**อย่าทำ**เพราะมันจะทำให้มีปัญหาเวลาใช้งานจริง
{% endhint %}

{% hint style="info" %}
**ตัวอักษรพิเศษ** เช่น !@#$%^&\*()\_+\\|/-+
{% endhint %}

{% hint style="info" %}
**คำสงวน** คือคำที่เราห้ามใช้ เพราะภาษานั้นๆจะขอเอาไว้ใช้เอง เช่นคำว่า int, string, double, class
{% endhint %}


---

# 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/basic/variable.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.
