Книга: 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.

Оглавление книги

Оглавление статьи/книги

Генерация: 0.152. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз