Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

การคอมไพล์และรันโปรแกรม C#: Command Line และ Visual Studio

การ Build และ Run โปรแกรม C# สามารถทำได้หลายวิธี ตั้งแต่ IDE เต็มรูปแบบจนถึง Command Line

วิธี 1: Visual Studio IDE (แนะนำสำหรับเริ่มต้น)

  1. เปิด Visual Studio → Create New Project → Console App
  2. เขียนโค้ดใน Program.cs
  3. กด F5 (Debug) หรือ Ctrl+F5 (Run Without Debugging)

วิธี 2: .NET CLI (Command Line) - Modern Way

# สร้างโปรเจกต์ใหม่
dotnet new console -n MyApp
cd MyApp

# Build
dotnet build

# Run
dotnet run

# Release Build
dotnet publish -c Release

วิธี 3: คอมไพล์โดยตรงด้วย CSC (Legacy)

  1. สร้างไฟล์ helloworld.cs ใน Text Editor
  2. เปิด Command Prompt / Terminal ไปที่โฟลเดอร์ไฟล์
  3. คอมไพล์: csc helloworld.cs
  4. สร้าง helloworld.exe
  5. รัน: helloworld
// helloworld.cs
using System;
class Program { static void Main() { Console.WriteLine("Hello World"); } }

วิธี 4: Online Compilers

โครงสร้างไฟล์ Build Output

MyApp/
├── bin/
│   └── Debug/
│       └── net8.0/
│           ├── MyApp.dll        # Assembly (IL Code)
│           ├── MyApp.exe        # Executable (Windows)
│           ├── MyApp.pdb        # Debug Symbols
│           └── MyApp.deps.json  # Dependencies
└── obj/                          # Temporary Build Files

Target Frameworks

  • net8.0 - .NET 8 (LTS, Cross-platform)
  • net48 - .NET Framework 4.8 (Windows Only)
  • netstandard2.0 - Library รองรับหลาย Platform

แนะนำ: ใช้ dotnet CLI สำหรับ CI/CD, Container, Cross-platform Development ใช้ Visual Studio / VS Code สำหรับ Development ปกติ