C#
C# – HelloWorld App
In this section we are going to see how we can create a HelloWorld App using C# in Visual Studio.
Creating a new console application (Visual Studio)
- Open Visual Studio.
- In the toolbar, go to File → New Project.
- Select the Console Application project type.
Click the Run button or press F5 key to execute the project. A Command Prompt window appears that contains the line HelloWorld.
Explanation
- 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 has the namespace declaration. A namespace is a collection of classes. The HelloWorldApp namespace contains the class Program.
- class Program is a class declaration. The class Program contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the Program class has only one method: Main.
- static void Main() defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed. Only one Main method is allowed per class.
- System.Console.WriteLine(“HelloWorld”); method prints a given data (in this example, Hello, world!) as an output in the console window.
- System.Console.ReadKey(), ensures that the program won’t close immediately after displaying the message. It does this by waiting for the user to press a key on the keyboard. Any key press from the user will terminate the program. The program terminates when it has finished the last line of code in the main() method.
Some Features of C#
- C# is case sensitive.
- All statements and expression must end with a semicolon (;).
- The program execution starts at the Main method.
- Unlike Java, program file name could be different from the class name.