Team LiB
Previous Section Next Section

Anatomy of a Simple C# Program

One of the simplest of all C# applications is shown in Figure 1-1.

Click To expand
Figure 1-1: Anatomy of a simple C# program

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

The "using System;" Statement

The first line of the program,

using System;

is required in order for our program to compile and run properly, by providing the compiler with knowledge of the types in the System namespace; a namespace is a logical grouping of predefined C# programming elements (in the case of C#, part of the FCL mentioned earlier). We'll defer a detailed explanation of namespaces until Chapter 13: for now, simply realize that the using System; statement is required for our 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, and which therefore may not 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; Appendix E presents the complete set of C# keywords.


The next line of our program is a comment:

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

C# supports three different comment styles; we'll review two of them here.

The C language style of block comment begins with a forward slash followed by an asterisk (/*) and ends 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;

The C++ single line form of comment uses a double slash (//) to comment just to the end of a line, as shown here:

x = y + z;    // text of comment continues through to the end of the line
a = b / c;
// Here is a BLOCK of sequential C++ style (single-line) comments.
// This serves as an alternative to using the C style
// of block comments (/* ... */).
m = n * p;

There is also a third type of C# comment that is used within XML document files, but that topic is beyond the scope of this book.

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;

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

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

Class Declaration/"Wrapper"

Next comes a class "wrapper"—more properly termed a class declaration—of the form

public class name
{ ... }


public class SimpleProgram
{ ... }

where braces, { }, enclose the main logic to be performed by the program, as well as enclosing other building blocks of a class; in later chapters, you'll learn all about classes, how to name them, and in particular why we even need a class wrapper in the first place. For now, simply note that the tokens public and class are two more of C#'s keywords, whereas SimpleProgram is a name that we've invented.

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.


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 of 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:

static void Main() {

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).

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


which prints the message


to the screen. 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 "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.

Team LiB
Previous Section Next Section