CHAPTER 3 CORE C# PROGRAMMING CONSTRUCTS, PART I
{
}
}
}
Given this, update the Main() method of your Program class with the following code statements:
class Program
{
static void Main(string[] args)
{
// Display a simple message to the user.
Console.WriteLine("***** My First C# App *****");
Console.WriteLine("Hello World!");
Console.WriteLine();
}
}
// Wait for Enter key to be pressed before shutting down.
Console.ReadLine();
Note C# is a case-sensitive programming language. Therefore, Main is not the same as main, and Readline is
not the same as ReadLine. Be aware that all C# keywords are lowercase (e.g., public, lock, class, dynamic),
while namespaces, types, and member names begin (by convention) with an initial capital letter and have
capitalized the first letter of any embedded words (e.g., Console.WriteLine, System.Windows.MessageBox,
System.Data.SqlClient). As a rule of thumb, whenever you receive a compiler error regarding “undefined
symbols,” be sure to check your spelling and casing first!
Here we have a definition for a class type that supports a single method named Main(). By default,
Visual Studio names the class defining Main() Program; however, you are free to change this if you so
choose. Every executable C# application (console program, Windows desktop program, or Windows
service) must contain a class defining a Main() method, which is used to signify the entry point of the
application.
Formally speaking, the class that defines the Main() method is termed the application object. While
it is possible for a single executable application to have more than one application object (which can be
useful when performing unit tests), you must inform the compiler which Main() method should be used
as the entry point via the /main option of the command-line compiler, or via the Startup Object dropdown list box, located under the Application tab of the Visual Studio project properties editor (see
Chapter 2).
Note that the signature of Main() is adorned with the static keyword, which will be examined in
detail in Chapter 5. For the time being, simply understand that static members are scoped to the class
level (rather than the object level) and can thus be invoked without the need to first create a new class
instance.
In addition to the static keyword, this Main() method has a single parameter, which happens to be
an array of strings (string[] args). Although you are not currently bothering to process this array, this
parameter may contain any number of incoming command-line arguments (you’ll see how to access
74