The Hello World! program is the most basic and first program when you dive into a new programming language. This simply prints the Hello World! on the output screen. In C#, a basic program consists of the following:
- A Namespace Declaration
- Class Declaration & Definition
- Class Members(like variables, methods etc.)
- Main Method
- Statements or Expressions
- using System: System is a namespace which contains the commonly used types. It is specified with a using System directive.
- namespace HelloWorldApp: Here namespace is the keyword which is used to define the namespace. HelloWorldApp is the user-defined name given to namespace. For more details, you can refer to C# | Namespaces
- class Geeks: Here class is the keyword which is used for the declaration of classes. Geeks is the user-defined name of the class.
- static void Main(string args): Here static keyword tells us that this method is accessible without instantiating the class. void keyword tells that this method will not return anything. Main() method is the entry point of our application. In our program, Main() method specifies its behavior with the statement Console.WriteLine(“Hello World!”);.
- Console.WriteLine(): Here WriteLine() is a method of the Console class defined in the System namespace.
- Console.ReadKey(): This is for the VS.NET Users. This makes the program wait for a key press and prevents the screen from running and closing quickly.
How to run a C# Program?
Generally, There are 3 ways to compile and execute a C# program as follows:
- To use an online C# compiler: You can use various online IDE. which can be used to run C# programs without installing.
- Using Visual Studio IDE: Microsoft has provided an IDE(Integrated Development Environment) tool named Visual Studio to develop applications using different programming languages such as C#, VB(Visual Basic) etc. To install and use Visual Studio for the commercial purpose it must buy a license from the Microsoft. For learning (non-commercial) purpose, Microsoft provided a free Visual Studio Community Version. To learn how to run a program in Visual Studio you can refer to this.
- Using Command-Line: You can also use command line options to run a C# program. Below steps demonstrate how to run a C# program on Command line in Windows Operating System:
- First, open a text editor like Notepad or Notepad++.
- Write the code in the text editor and save the file with .cs extension.
- Open the cmd(Command Prompt) and run the command csc to check for the compiler version. It specifies whether you have installed a valid compiler or not. You can avoid this step if you confirmed that compiler is installed.
- To compile the code type csc filename.cs on cmd. If your program has no error then it will create a filename.exe file in the same directory where you have saved your program. Suppose you saved the above program as hello.cs. So you will write csc hello.cs on cmd. This will create a hello.exe.
- Now you have to ways to execute the hello.exe. First, you have to simply type the filename i.e hello on the cmd and it will give the output. Second, you can go to the directory where you saved your program and there you find filename.exe. You have to simply double-click that file and it will give the output.