Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

64

Part II

HANDLING DATA

 

 

 

Writing, Compiling, and Executing

a C# Program

Writing a C# Program

Writing a program in C# involves writing the Main() method. Before w riting a program, you need to select the template from the available templates for C#. C# provides you with a variety of templates, as shown in Figure 3-2.

FIGURE 3-2 Templates for writing a C# program

The execution of a C# program starts with the execution of the Main() method. Therefore, you need to write the Main() method for each program in C#. The Main() method is of the type static and returns a value of the type void or int.

If the Main() method is of the type void, it does not return a value. However, a Main() method of the type integer returns an integer type variable. The following is the syntax of a Main() method.

<modifier> static <data type> Main ()

Here, modifier is the access modifier of the Main() method and the data type is

void or integer.

COMPONENTS OF C#

Chapter 3

65

 

 

 

The modifier of the Main() method is explicitly written as public. However, it would not make a difference if any other modifier is specified.

The next code is an example of writing a simple program in C#.

using System; class Class1

{

public static void Main()

{

Console.WriteLine (“This is a sample program in C#”);

}

}

The code uses the using statement to enable you to use the System namespace in the program code.The class keyword is then used to declare a class by the name Class1. Inside the class declaration is the static method Main() of the type void. The Console.WriteLine statement is used to display the text given in double quotes (“ ”) in the Console window.

Compiling a C# Program

Once you have written a program, you can compile the program by using the Build command in the Build menu. The compilation of the program is shown in Figure 3-3.

FIGURE 3-3 Compiling a C# prog ram