Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
77
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

Now to compile and execute your application, select Debug - Start Without Debugging or press Ctrl+F5. This will open a new Console Window with Hello World written in it. Once you press any key, the window will close, terminating the program.

Understanding the Hello World Application Code:

The first line of our program (using System;) appears in virtually all the C# programs. It gives us access to the core functionality of our computer system. We will discuss this a bit later. Let's first see what the second line (namespace MyHelloWorldApplication) means.

Namespaces in C#

A Namespace is simply a logical collection of related classes in C#. We bundle our related classes (like those related with database activity) in some named collection calling it a namespace (e.g., DataActivity). As C# does not allow two classes with the same name to be used in a program, the sole purpose of using namespaces is to prevent name conflicts. This may happen when you have a large number of classes, as is the case in the Framework Class Library (FCL). It is very much possible that our Connection Class in DataActivity conflicts with the Connection Class of InternetActivity. To avoid this, these classes are made part of their respective namespace. So the fully qualified name of these classes will be DataActivity.Connection and InternetActivity.Connection, hence resolving any ambiguity for the compiler.

So, in the second line of our program we are declaring that the following classes (within { } block) are part of MyHelloWorldApplication namespace.

namespace MyHelloWorldApplication

{

...

}

The C# namespaces have NO physical mapping as is the case in Java. Classes with same namespace can be in different folders. The C# concept of mapping is similar to packages in Java and namespaces in standard C++. The namespace may contain classes, events, exceptions, delegates and even other namespaces called 'Internal namespace'.

25

Programmers Heaven: C# School

These internal namespaces can be defined like this:

namespace Parent

{

namespace Child

{

...

}

}

The using Keyword

The first line of our program was:

using System;

The using keyword above allows us to use the classes in the following 'System' namespace. By doing this, we can now access all the classes defined in the System namespace like we are able to access the Console class in our Main method later. One point to remember here is using allows you to access the classes in the referenced namespace only and not in its internal/child namespaces. Hence we might need to write

using System.Collections;

in order to access the classes defined in Collection namespace which is a sub/internal namespace of System namespace.

The class Keyword

All of our C# programs contain at least one class. The Main() method resides in one of these classes. Classes are a combination of data (fields) and functions (methods) that can be performed on this data in order to achieve the solution to our problem. We will see the concept of class in more detail in the coming days. Classes in C# are defined using the class keyword followed by the name of class.

The Main() Method

In the next line we defined the Main() method of our program:

static void Main(string[] args)

This is the standard signature of the Main method in C#. The Main method is the entry point of our program, i.e., our C# program starts its execution from the first line of Main method and terminates with the termination of Main method. The Main method is designated as static as it will be called by the Common Language Runtime (CLR)

26

Programmers Heaven: C# School

without making any object of our HelloWorld Class (which is the definition of static methods, fields and properties). The method is also declared void as it does not return anything. Main is the (standard) name of this method, while string [] args is the list of parameters that can be passed to main while executing the program from command line. We will see this later.

One interesting point here is that it is legitimate to have multiple Main() methods in C# program. But, you have to explicitly identify which Main method is the entry point at the run-time. C++ and Java Programmers, take note that Main starts with capital 'M' and the return type is void.

Printing on the Console

Our next line prints Hello World on the Console screen:

Console.WriteLine("Hello World");

Here we called WriteLine(), a static method of the Console class defined in the System namespace. This method takes a string (enclosed in double quotes) as its parameter and prints it on the Console window.

C#, like other Object Oriented languages, uses the dot (.) operator to access the member variables (fields) and methods of a class. Also, braces () are used to identify methods in the code and string literals are enclosed in double quotation marks ("). Lastly, each statement in C# (like C, C++ and Java) ends with a semicolon (;), also called the statement terminator.

Comments

Comments are the programmer's text to explain the code, are ignored by the compiler and are not included in the final executable code. C# uses syntax for comments that is similar to Java and C++. The text following double slash marks (// any comment) are line comments. The comment ends with the end of the line:

// This is my main method of program

static void Main()

{

...

}

C# also supports the comment block. In this case, the whole block is ignored by the compiler. The start of the block is declared by slash-asterisk (/*) and ends with asterisk-slash mark (*/):

static void Main()

{

/* These lines of text

will be ignored by the compiler */

...

27

Programmers Heaven: C# School

}

C# introduces another kind of comment called 'documentation comments'. C# can use these to generate the documentation for your classes and program. These are line comments and start with triple slash mark (///):

/// These are documentation comments

We will discuss these in detail in coming issues.

Important points to remember

Your C# executable program resides in some class.

The entry point to program is the static method Main() with void return type

C# is a case sensitive language so void and Void are different

Whitespaces (enter, tab, space) are ignored by the compiler between the code. Hence, the following is also a valid declaration of the Main() method although it is not recommended:

static void

Main ( )

{

...

}

You DON'T need to save your program with same file name as of your class containing Main() method

There can be multiple Main() methods in your program

The boundaries of namespace, class and method are defined by opening and closing curly brackets { }

A namespace is only logical collection of classes with no physical mapping on disk (unlike Java)

The using keyword is used to inform compiler where to search for the definition of classes (namespaces) that you are about to use in your C# program.

The three types of comments exist in C#; line, block and documentation. These are ignored by the compiler and are used only to enhance the readability and understandability of program for the developers.

Enclosing your class in some namespace is optional. You can write program where your class is not enclosed by any namespace

It is not mandatory that Main Method of program takes 'string [] args' as parameter. It is perfectly valid to write Main method as:

static void Main()

{

...

}

28

Соседние файлы в папке books