Section 2.3. Examining Your First Program

   

2.3 Examining Your First Program

The single greatest challenge when learning to program is that you must learn everything before you can learn anything. Even this simple "Hello World" program uses many features of the language that will be discussed in coming chapters, including classes, namespaces, statements, static methods , objects, strings, inheritance, blocks, libraries, and even something called polymorphism!

It is as if you were learning to drive a car. You must learn to steer, accelerate, brake, and understand the flow of traffic. Right now we're going to get you out on the highway and just let you steer for a while. Over time you'll learn how to speed up and slow down. Along the way you'll learn to set the radio and adjust the heat so that you'll be more comfortable. In no time you'll be driving, and then won't your parents begin to worry.

2.3.1 Line-by-Line Analysis

Hang on tight, we're going to zip through this quickly and come back to the details in subsequent chapters.

The first line in the program defines a namespace :

  namespace NotePad  

You will create many names when programming in C#. Every object and every type of object must be named. It is possible for the names you assign to conflict with the names assigned by Microsoft or other vendors . A namespace is a way to say "these names are mine."

In this program, you've created a namespace called NotePad. Each namespace must be enclosed in braces ( {} ). Thus, the second line of the Hello World program is an open brace to mark the beginning of the NotePad namespace. The open brace is matched by a closing brace at the end of the program.

Within the braces of the namespace, you write other programming constructs. For instance, you might define what is called an object. Every object named within these braces is implicitly prefixed with the name NotePad. The dot operator (.) separates the namespace from the name of the object within the namespace. Thus, if you were to create an object MyObject within the namespace NotePad, the real name of that object would be NotePad.MyObject. You can read this as either "NotePad dot MyObject" or "NotePad MyObject". Actually, you use the dot operator quite a lot; you'll see various other uses as we proceed.

Classes define a category, or type, of object. In C# there are thousands of classes. A class is a new, user -defined type. Classes are used to define Windows controls ( buttons , list boxes, etc.), as well as types of things ( employees , students, telephones, etc.) in the program you are writing. Some classes you create yourself, and some you obtain from the .NET Framework. Each class must be named.

Classes are the core of C# and object-oriented programming. You'll learn about classes in detail in Chapter 3, as well as in Chapter 5.

The third line in our Hello World program creates a class named, aptly, HelloWorld. Like a namespace, a class is defined within braces. The following code represents the opening of the HelloWorld class definition:

 class HelloWorld { 

A method is a small block of code that performs an action. The Main() method is the "entry point" for every C# console application; it is where your program begins. The next few lines in Hello World mark the beginning of the Main() method:

 static void Main() { 

Methods are covered in detail in Chapter 9 but are mentioned in virtually every chapter in this book.

A comment (here in bold) appears just before the start of the Main() method:

  // every console app starts with Main  static void Main() { 

A comment is just a note to yourself. You insert comments to make the code more readable to programmers. You can place comments anywhere in your program that you think the explanation will be helpful; they have no effect on the running program.

C# recognizes three styles of comments. The comment in Hello World begins with two slashes (//). The slashes indicate that everything to the right on the same line is a comment.

The second style is to begin your comment with a forward slash followed by an asterisk ( /* ) and to end your comment with the opposite pattern ( */ ). These pairs of characters are called the opening C-style comment and the closing C-style comment, respectively.

These comment symbols were inherited from the C language ” thus the names used to identify them. They are also used in C++ and Java.

Everything between these comment symbols is a comment. C-style comments can span more than one line, as in the following:

 /* This begins a comment This line is still within the comment Here comes the end of the comment */ 

The third and final style of comments uses three forward slashes ///. This is an XML-style comment and is used for advanced documentation techniques. XML comments are beyond the scope of this book.

Notice that the Main() method is defined with the keywords static and void .

  static  void Main() 

The static keyword indicates that you can access this method without having an object of your class available. While a class defines a type, each instance of that type is an object (much as Car defines a type of vehicle and your aging rust-bucket is an individual instance of Car). Thus, while Button defines a type of control for a Windows program, any individual program will have many Button objects, each with its own label (e.g., OK, Cancel, Retry).

Normally, methods can be called only if you have an object, but static methods are special and can be called without an object. (The use of static methods, other than Main(), is fairly advanced and won't be covered until Chapter 8.)

The second keyword in the statement defining the Main() method is void :

 static  void  Main() 

Typically, one method calls another. The called method will do work, and it can return a value to the calling method. (You'll see how methods call one another and return values in Chapter 9.) If a method does not return a value, it is declared void. The keyword void is a signal to the compiler that your method will not return a value to the calling method.

The operating system calls Main() (when the program is invoked). It is possible for Main() to return a value (typically an error code) that might be used in a batch file. In this case, you've declared that Main() will not return a value since you will not be calling this program from a batch file. Every method name is followed by parentheses

 static void Main  ()  

It is possible to pass values into a method so that the method can manipulate or use those values. These values are called parameters or arguments. (Method parameters are covered in Chapter 9.) In this case, Main() has no parameters. All methods are enclosed within braces. Within the braces for Main() is a single line of code:

 System.Console.WriteLine("Hello world!"); 

The Console is an object that represents your screen. The Console class is defined within the System namespace, and so its full identification is System.Console.

The Console class has a static method, WriteLine(), which you access not with an instance of Console, but through the Console class itself. Since you access the method with the dot operator, you write System.Console.WriteLine.

The WriteLine() method declares a single parameter: the string you want to display. When you pass a string in to the method, the string is an argument. The argument ("Hello world") corresponds to the parameter the method expects, and the string is displayed. The complete call to the method is:

  System.Console  .WriteLine("Hello world!"); 

If you will use many objects from the System namespace, you can save typing by telling the compiler that many of the objects you'll refer to are in that namespace. You do so by adding a using declaration to the beginning of your program:

 using System; 

Once you add this line, you can use the Console class name without explicitly identifying that it is in the System namespace. If you add the using declaration, you can rewrite the contents of Main() as follows :

 Console.WriteLine("Hello world!"); 

The final series of lines close the various nested opening braces. The first closes the brace for Main(), the second closes the brace for the class, and the third closes the brace for the namespace. Each open brace must be matched by a closing brace.

The class is defined within the namespace declaration, and thus you do not close the namespace until you've closed the class. Similarly, the method Main() is declared within the class, so you do not close the class until you've closed the method.

Whew! That was a lot to take in all at once! Don't panic, all of the concepts introduced here are explained in detail in coming chapters.

   


Learning C#
Learning C# 3.0
ISBN: 0596521065
EAN: 2147483647
Year: 2005
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net