Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Viết Hello World trong C#

Chương trình C# cơ bản in "Hello World":

Code

using System;

namespace Program
{
    class MyApplication
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Console.Read();
        }
    }
}

Kết quả

Hello World

Giải thích:

  • using System: Import namespace chứa Console
  • namespace: Tổ chức code
  • class: Container cho method
  • static void Main: Entry point
  • Console.WriteLine: In dòng + newline
  • Console.Read(): Đợi phím (giữ console mở)

Top-level statements (.NET 6+)

// Program.cs - không cần class, namespace, Main
Console.WriteLine("Hello World");

Compiler tự generate Main. Rất tiện cho script, tutorial, prototype.