1.4. Anatomy of a Simple C# Program

One of the simplest of all C# applications, the classic "Hello" program, is shown in Figure 1-1.

Figure 1.1. Anatomy of a simple C# program

Let's go over the key elements of our simple program.

1.4.1. The using System; Statement

The first line of the program is required for our program to compile and run properly, by providing the compiler with knowledge of the types in the System namespace, which is a logical grouping of predefined C# programming elements (in the case of C#, part of the FCL mentioned earlier):

using System;

We'll defer a detailed explanation of namespaces until Chapter 13; for now, simply realize that the using System; statement is required for this program to compile properly—specifically, for the line Console.WriteLine("Hello!"); to compile properly.

using is a C# keyword. Keywords, also known as reserved words, are tokens that have special meaning in a language, so they cannot be used by programmers as the names of variables, functions, or any of the other C# building blocks that you'll be learning about. We'll encounter many more C# keywords throughout the book.

1.4.2. Comments

The next line of our program is a single-line comment:

// This simple program illustrates some basic C# syntax.

In addition to single-line comments, the C# language also supports the C language style of delimited comments, which can span multiple lines. Delimited comments begin with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/). Everything enclosed between these delimiters is treated as a comment and is therefore ignored by the compiler, no matter how many lines the comment spans:

/* This is a single line C-style comment. */

/* This is a multiline C-style comment. This is a handy way to temporarily
   comment out entire sections of code without having to delete them.
   From the time that the compiler encounters the first 'slash asterisk'
   above, it doesn't care what we type here; even legitimate lines of code,
   as shown below, are treated as comment lines and thus ignored by the
   compiler until the first 'asterisk slash' combination is encountered.
x = y + z;
a = b / c;
j = s + c + f;
*/

NOTE

There is also a third type of C# comment that is used within XML document files. XML documentation comments are denoted by three slashes (///).

Note that comments can't be nested; that is, the following will not compile:

/* This starts a comment...
x = 3;

/* Whoops!  We are mistakenly trying to nest a SECOND comment
before terminating the FIRST!
This is going to cause us compilation problems, because the
compiler is going to IGNORE the start of this second/inner comment - we're IN
a comment already, after all! - and so as soon as we try to terminate
this SECOND/inner comment, the compiler will think that we've terminated the
FIRST/outer comment instead... */
z = 2;
 */ ...then, when we try to terminate the FIRST/outer comment,
            the compiler will inform us that THIS line of code is invalid.

When the compiler reaches what we intended to be the terminating */ of the "outer" comment in the last line of code, the following compiler error will be reported:

error: Invalid expression term '/'
error: ; expected

1.4.3. Class Declaration/"Wrapper"

Next comes a class "wrapper"—more properly termed a class declaration—of the following form where braces, {...}, enclose the main logic to be performed by the class, as well as enclosing other building blocks of a class:

class name
{...}

e.g.,

class SimpleProgram
{...}

In later chapters, you'll learn all about classes, how to name them, and in particular why you even need a class wrapper in the first place. For now, simply note that the token class is another one of C#'s keywords, whereas SimpleProgram is a name that we invented.

1.4.4. Main Method

Within the SimpleProgram class declaration, we find the starting function for the program, called the Main method in C#. The Main method serves as the entry point for a C# program. When the program executable is invoked, the system will call the Main method to launch our application.

NOTE

With trivial applications such as this simple example, all logic can be contained within this single method. For more complex applications, on the other hand, the Main method can't possibly contain all the logic for the entire system. You'll learn how to construct an application that transcends the boundaries of the Main method later in the book.

The first line of the method, shown here, defines what is known as the Main method's header, and must appear exactly as shown (with one minor exception that we'll explain in Chapter 13 having to do with optionally receiving arguments from the command line):

static void Main() {

Our Main method body, enclosed in braces, {...}, consists of a single statement:

Console.WriteLine("Hello!");

This statement prints the following message to the screen:

Hello!

We'll talk more about this statement's syntax in a bit, but for now note the use of a semicolon at the end of the statement. As in C, C++, and Java, semicolons are placed at the end of individual C# statements. Braces {...} delimit blocks of code, the significance of which we'll discuss in more detail later in this chapter, in the section entitled "Code Blocks and Variable Scope."

Other things that we'd typically do inside of the Main method of a more elaborate program include declaring variables, creating objects, and calling other methods.

Now that we've looked at a simple C# program, let's explore some of the basic syntax features of C# in more detail.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset