C# Program Structure
Before we delve into the fundamental building blocks of the C# programming language, let's first look at the minimal structure of a C# program to serve as a reference for the upcoming chapters.
C# Hello World Example
A C# program generally includes the following parts:
- Namespace declaration
- A class
- Class methods
- Class attributes
- A Main method
- Statements & Expressions
- Comments
The file extension for C# files is .cs
.
Below is a test.cs file that contains simple code to print "Hello World":
test.cs File Code:
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* My first C# program */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
Hello World
Let's examine the various parts of the program:
- The first line using System; - The using keyword is used to include the System namespace in the program. A program typically has multiple using statements.
- The next line is a namespace declaration. A namespace contains a set of classes. The HelloWorldApplication namespace contains the class HelloWorld.
- The next line is a class declaration. The class HelloWorld contains the data and method definitions used by the program. A class usually contains multiple methods. Methods define the behavior of the class. Here, the HelloWorld class has only one Main method.
- The next line defines the Main method, which is the entry point for all C# programs. The Main method specifies the actions the class will perform when executed.
- The line /.../ will be ignored by the compiler and it adds additional comments to the program.
- The Main method specifies its behavior with the statement Console.WriteLine("Hello World");.
WriteLine is a method of the Console class defined in the System namespace. This statement displays the message "Hello World" on the screen.
- The last line Console.ReadKey(); is for VS.NET users. This makes the program wait for a key press, preventing the screen from quickly running and closing when launched from Visual Studio .NET.
A few important points to note:
- C# is case-sensitive.
- All statements and expressions must end with a semicolon (;).
- Program execution starts from the Main method.
- Unlike Java, the file name can be different from the class name.
Compiling & Executing C# Program
If you are using Visual Studio.Net to compile and execute a C# program, follow these steps:
- Start Visual Studio.
- On the menu bar, choose File -> New -> Project.
- Select Visual C# from the templates, then choose Windows.
- Choose Console Application.
- Name your project and click OK.
- The new project will appear in the Solution Explorer.
- Write the code in the code editor.
- Click the Run button or press F5 to run the program. A command prompt window will appear, displaying "Hello World".
You can also compile a C# program using the command line instead of Visual Studio IDE:
- Open a text editor and add the code mentioned above.
- Save the file as helloworld.cs.
- Open the command prompt tool and navigate to the directory where the file is saved.
- Type csc helloworld.cs and press enter to compile the code.
- If there are no errors, the command prompt will move to the next line and generate the helloworld.exe executable file.
- Next, type helloworld to execute the program.
- You will see "Hello World" printed on the screen.