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

คอมไพล์และรัน C# บน Windows: Visual Studio

ภาพรวม

Visual Studio เป็น IDE หลักสำหรับพัฒนา C# บน Windows รองรับ .NET Framework, .NET Core, .NET 5+ พร้อมเครื่องมือครบครัน

คุณสมบัติหลักของ Visual Studio

  • IntelliSense: Code completion, parameter info, quick info
  • Debugger: Breakpoints, watch windows, call stack, immediate window
  • Refactoring: Rename, extract method, inline, आदि
  • Git Integration: Built-in source control
  • NuGet Package Manager: จัดการ dependencies
  • Unit Test Explorer: รัน/ดูผล Test ทั้ง solution
  • Live Share: Real-time collaboration

ขั้นตอนสร้างและรัน Console App

  1. เปิด Visual Studio → Create a new project
  2. ค้นหา "Console App" → เลือก Console App (C#) ที่มี Tag .NET Core หรือ .NET 6/7/8
  3. Click Next
  4. ตั้งชื่อ Project (เช่น HelloWorld) → Next
  5. เลือก Target Framework (แนะนำ .NET 8.0 LTS) → Create
  6. โค้ดจะปรากฏใน Program.cs
  7. กด F5 (Start Debugging) หรือ Ctrl+F5 (Start Without Debugging)

โค้ด Hello World (.NET 6+ Top-level Statements)


// Program.cs
Console.WriteLine("Hello, World!");

โครงสร้างโปรเจกต์


HelloWorld/
├── HelloWorld.csproj        # Project file (SDK-style)
├── Program.cs               # Entry point
├── obj/                     # Build intermediates
└── bin/                     # Build outputs
    └── Debug/net8.0/
        ├── HelloWorld.exe
        ├── HelloWorld.dll
        └── HelloWorld.pdb

Command Line (dotnet CLI)


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

# รัน
dotnet run

# Build
dotnet build

# Build Release
dotnet build -c Release

# Publish (Self-contained)
dotnet publish -c Release -r win-x64 --self-contained true

Visual Studio Editions

EditionPriceTarget
CommunityFreeIndividual, Open Source, Small Teams
ProfessionalPaidProfessional Developers
EnterprisePaidLarge Teams, Advanced Testing/Architecture

Alternative IDEs บน Windows

  • VS Code + C# Dev Kit Extension (Lightweight, Cross-platform)
  • JetBrains Rider (Paid, Cross-platform, Excellent Refactoring)
  • Visual Studio Code + OmniSharp (Free, Open Source)