1️⃣ C# คืออะไร?

By suparat · 08 Oct 2025


ได้เลย 😎 เรามาเริ่ม C# Introduction ตั้งแต่พื้นฐานจนเข้าใจโครงสร้างหลักของภาษาแบบเข้าใจง่าย


1️⃣ C# คืออะไร?

  • C# (ซีชาร์ป) เป็นภาษาโปรแกรม Modern, Type-safe, Object-Oriented

  • พัฒนาโดย Microsoft ใช้กับ .NET Framework / .NET Core / .NET 5+

  • เหมาะกับ:

    • Desktop App → WPF, WinForms

    • Web App → ASP.NET Core

    • Mobile App → Xamarin / MAUI

    • Game → Unity

    • Cloud → Azure Functions


2️⃣ โครงสร้างโปรแกรม C# พื้นฐาน

using System; // Namespace

namespace MyFirstApp
{
    class Program
    {
        static void Main(string[] args) // Entry Point
        {
            Console.WriteLine("Hello, C#!"); // แสดงผล
        }
    }
}

คำอธิบาย:

  • using System; → นำ Namespace เข้ามาใช้

  • namespace → จัดกลุ่ม Class

  • class Program → ประกาศคลาส

  • static void Main() → จุดเริ่มต้นของโปรแกรม

  • Console.WriteLine() → แสดงข้อความ


3️⃣ ตัวแปรและชนิดข้อมูล (Data Types)

ชนิดข้อมูล ตัวอย่าง คำอธิบาย
int int age = 25; จำนวนเต็ม 32-bit
long long big = 100000; จำนวนเต็ม 64-bit
float float pi = 3.14f; ทศนิยม 32-bit
double double pi2 = 3.14159; ทศนิยม 64-bit
decimal decimal money = 100.5m; ทศนิยมแม่นยำสูง
bool bool isActive = true; true/false
string string name = "Jen"; ตัวอักษร / ข้อความ
char char grade = 'A'; ตัวอักษรเดียว

4️⃣ Operator พื้นฐาน

int a = 10, b = 3;
Console.WriteLine(a + b); // 13
Console.WriteLine(a - b); // 7
Console.WriteLine(a * b); // 30
Console.WriteLine(a / b); // 3
Console.WriteLine(a % b); // 1 (modulus)

Comparison & Logical Operators

Console.WriteLine(a > b); // True
Console.WriteLine(a == b); // False
Console.WriteLine(a != b && b < a); // True

5️⃣ Control Flow

if-else

int score = 80;
if(score >= 90)
    Console.WriteLine("A");
else if(score >= 75)
    Console.WriteLine("B");
else
    Console.WriteLine("C");

switch

string grade = "B";
switch(grade)
{
    case "A": Console.WriteLine("Excellent"); break;
    case "B": Console.WriteLine("Good"); break;
    default: Console.WriteLine("Needs Improvement"); break;
}

loop

for(int i = 0; i < 5; i++)
    Console.WriteLine(i);

int j = 0;
while(j < 5)
{
    Console.WriteLine(j);
    j++;
}

6️⃣ Method / Function

static int Add(int x, int y)
{
    return x + y;
}

Console.WriteLine(Add(5, 3)); // 8
  • Parameters → ค่าที่ส่งเข้าไป

  • Return → ค่าที่ส่งกลับ


7️⃣ Class & Object

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public void SayHello()
    {
        Console.WriteLine($"Hello, my name is {Name}");
    }
}

var person = new Person { Name = "Jen", Age = 25 };
person.SayHello(); // Hello, my name is Jen

8️⃣ Encapsulation (Access Modifiers)

Modifier การเข้าถึง
public เข้าถึงได้ทุกที่
private เข้าถึงได้เฉพาะคลาส
protected เข้าถึงได้ในคลาสและ subclass
internal เข้าถึงได้ใน assembly เดียวกัน

9️⃣ Collection พื้นฐาน

// List
var fruits = new List<string> { "Apple", "Banana" };
fruits.Add("Orange");

// Dictionary
var users = new Dictionary<int, string> { {1, "Jen"}, {2, "Mai"} };

// Loop ผ่าน List
foreach(var fruit in fruits)
    Console.WriteLine(fruit);

🔟 Exception Handling

try
{
    int x = 0;
    Console.WriteLine(10 / x);
}
catch(DivideByZeroException ex)
{
    Console.WriteLine("Cannot divide by zero!");
}
finally
{
    Console.WriteLine("Always executed");
}

💡 สรุป:

  • C# เป็น Object-Oriented Language

  • มี Type-safe, Strongly-typed, Rich Library

  • ใช้ได้กับหลาย Platform (Desktop, Web, Mobile, Cloud, Game)

  • พื้นฐานสำคัญ: Variables, Control Flow, Loops, Methods, Class & Object, Collections, Exception


 

1️⃣ C# คืออะไร?
Web hosting by Somee.com