YouTip LogoYouTip

Csharp Program Structure

# C# Program Structure Before we start learning the basic building blocks of the C# programming language, let us look at the smallest possible C# program structure so that it can be used as a reference for the subsequent chapters. !(#) ## C# Hello World Example A C# program consists of the following parts: * Namespace declaration * A class * Class methods * Class attributes * A Main method * Statements & Expressions * Comments C# files have the extension .cs. Let us create a file **test.cs** with the following code that prints "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 us look at the various parts of the program: * The first line of the program **using System;** - The **using** keyword is used to include the **System** namespace in the program. A program generally has multiple **using** statements. * The next line is the **namespace** declaration. A **namespace** is a collection of classes. The _HelloWorldApplication_ namespace contains the class _HelloWorld_. * The next line is the **class** declaration. The class _HelloWorld_ contains the data and method declarations that the program uses. Classes generally contain 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 states what the class will do when executed. * The next line /*...*/ is ignored by the compiler and it is put there to add an extra **comment** to the program. * The Main method specifies its behavior through the statement **Console.WriteLine("Hello World");**. _WriteLine_ is a method of the _Console_ class defined in the _System_ namespace. This statement causes the message "Hello World" to be displayed on the screen. * The last line **Console.ReadKey();** is for the VS.NET users. This makes the program wait for a key press and prevents the screen from running and closing quickly when started from Visual Studio .NET. A few 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 a C# Program If you are using Visual Studio.Net to compile and execute a C# program, please follow the steps below: * Start Visual Studio. * On the menu bar, choose File -> New -> Project. * From templates, choose Visual C# and then choose Windows. * Choose Console Application. * Specify a name for your project and click the OK button. * The new project will appear in the Solution Explorer. * Write the code in the Code Editor. * Click the Run button or press F5 key to run the program. A Command Prompt window will appear that will show Hello World. You can also compile a C# program using the command line instead of the 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 go to the directory where you saved your file. * Type **csc helloworld.cs** and press enter to compile the code. * If there are no errors in your code, the command prompt will take you to the next line and will generate **helloworld.exe** executable file. * Next, type **helloworld** to execute the program. * You will see "Hello World" printed on the screen.
← Csharp Basic SyntaxCsharp Environment Setup β†’