Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 25 OTHER TOPICS

Method Main

Every C# program must have one entry point—a method that must be called Main.

In the sample code throughout this text, I’ve used a version of Main that takes no parameters and returns no value. There are, however, four forms of Main that are acceptable as the entry point to a program. These forms are the following:

static void Main()

{...

}

static void Main( string[] args) {...}

static int Main()

{...

}

static int Main( string[] args) {...}

The first two forms don’t return a value to the execution environment when the program terminates. The second two forms return an int value. A return value, if one is used, is generally used to report success or failure of the program, where 0 is generally used to indicate success.

The second and fourth forms allow you to pass actual parameters, also called arguments, from the command line into the program, when it starts. Some important characteristics of command-line arguments are the following:

There can be zero or more command-line arguments. Even if there are no arguments, the args parameter is not null. Instead, it is an array with no elements.

The arguments are separated by spaces or tabs.

Each argument is interpreted by the program as a string, but you don’t need to enclose them in quotation marks on the command line.

For example, the following program, called CommandLineArgs, accepts command-line arguments and prints out each argument supplied:

class Program

{

static void Main(string[] args)

{

foreach (string s in args) Console.WriteLine(s);

}

}

The following command line executes program CommandLineArgs with five arguments:

CommandLineArgs Jon Peter Beth Julia Tammi

Executable

Arguments

Name

 

679

CHAPTER 25 OTHER TOPICS

The preceding program and command line produce the following output:

Jon

Peter

Beth

Julia

Tammi

Other important things to know about Main are the following:

Main must always be declared static.

Main can be declared in either a class or a struct.

A program can contain only one declaration of the four acceptable entry point forms of Main. You can, however, legally declare other methods named Main, as long as they don’t have any of the four entry point forms—but doing this is inviting confusion.

Accessibility of Main

Main can be declared public or private:

If Main is declared private, other assemblies cannot access it, and only the execution environment can start the program.

If Main is declared public, other assemblies can call it.

The execution environment, however, always has access to Main, regardless of its declared access level or the declared access level of the class or struct in which it is declared.

By default, when Visual Studio creates a project, it creates a program outline where Main is implicitly private. You can always add the public modifier if you need to do so.

680

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]