Để truyền tham số qua dòng lệnh, dùng tham số string[] args của phương thức Main.
Cấu trúc Main với tham số:
class HelloWorld
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
Biến args chứa tất cả giá trị truyền từ dòng lệnh.
In ra các tham số:
Console.WriteLine("Length of the arguments: " + args.Length);
Console.WriteLine("Arguments:");
foreach (object obj in args)
{
Console.WriteLine(obj);
}
Ví dụ chạy: MyApp.exe One Two Three
Length of the arguments: 3
Arguments:
One
Two
Three
Lưu ý: Trong Visual Studio, set command line arguments tại Project Properties > Debug > Application arguments.