Saturday, October 10, 2009

The Anatomy of a Simple C# Program

C# demands that all program logic be contained within a type definition type is a general term referring to a member of the set {class, interface, structure, enumeration, delegate}).
Unlike many other languages, in C# it is not possible to create global functions or global
points of data. Rather, all data members and methods must be contained within a type definition.
To get the ball rolling, create a new Console Application project named SimpleCSharpApp. As you
might agree, the initial code statements are rather uneventful:
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace SimpleCSharpApp
{
class Program
{
static void Main(string[] args)
{
}
}
}
Given this, update the Main() method 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();
}
}
Here, we have a definition for a class type that supports a single method named Main(). By
default, Visual Studio 2008 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 informthe compiler which Main() method should be used as the entry point via the /main option of the command-line compiler. 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.
Note C# is a case-sensitive programming language. Therefore, “Main” is not the same as “main,” and
“Readline” is not the same as “ReadLine.” Given this, be aware that all C# keywords are lowercase (public,
lock, class, global, and so on), 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.Forms.MessageBox, System.Data.SqlClient, and so on). As a rule of thumb, whenever you receive a compiler error regarding “undefined symbols,” be sure to check your spelling! 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 them momentarily). Finally, this Main() method has been set up with a void return value, meaning we do not explicitly define a return value using the return keyword before exiting the method scope. The logic of Program is within Main() itself. Here, you make use of the Console class, which is defined within the System namespace. Among its set of members is the static WriteLine() which, as
you might assume, pumps a text string and carriage return to the standard output. You also make a call to Console.ReadLine() to ensure the command prompt launched by the Visual Studio 2008 IDE remains visible during a debugging session until you press the Enter key.

No comments:

Post a Comment