Книга: C# 2008 Programmer
Passing Arguments to Main()
Passing Arguments to Main()
If you run a program in the command prompt as described earlier in the chapter, you can pass in arguments to the application. For example, you might want the program to display your name. To do so, pass in the name like this:
C:C#>HelloWorld Wei-Meng Lee
The argument passed into the program can be accessed by the args parameter (a string array) defined in the Main() method. Hence, you need to modify the program by displaying the values contained in the args string array, like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld {
class Program {
static void Main(string[] args) {
Console.Write("Hello, ");
for (int i = 0; i < args.Length; i++)
Console.Write("{0} ", args[i]);
Console.Write("! This is my first C# program!");
Console.ReadLine();
return;
}
}
}
Chapter 8 covers string arrays in depth.
- 4.1.1 Function Prototypes
- 4.1.3 Passing Variables by Reference to Functions
- 4.1.4 Variable Number of Arguments
- MainWindow
- Your Own Domain Name and Third-Party DNS
- Providing DNS for a Real Domain with BIND
- Providing DNS for a Real Domain
- Registering the Domain
- Maintaining Accurate Serial Numbers
- Using Single Quotes to Maintain Unexpanded Variables
- Maintaining yum
- 5.2.2. Kernel Startup: main.c