C # Hello World instance
A C # program mainly includes the following sections:
- namespace declaration (Namespace declaration)
- A class
- Class method
- Class Property
- A Main method
- Statements (statements) & Expressions (Expressions)
- Comments
Let's look at a simple code that can print out "Hello World":
Using System;Namespace Helloworldapplication{ Class HelloWorld { static main (string[]< Span class= "PLN" > Args) {/* my first C # program */ console. Writeline "Hello World" console. Readkey} }} /span>
When the above code is compiled and executed, it produces the following results:
Hello World
Let's take a look at the various parts of the above program:
The following points are worth noting:
- C # is case-sensitive.
- All statements and expressions must end with a semicolon (;).
- The execution of the program starts with the Main method.
- Unlike Java, a file name can be different from the name of a class.
Compiling & executing C # programs
If you are using Visual Studio.NET to compile and execute a C # program, follow these steps:
- Start Visual Studio.
- On the menu bar, select Project, File, New.
- Select Visual C # from the template, and then select Windows.
- Select Console Application.
- Make a name for your project, and then click the OK button.
- The new project appears in Solution Explorer (Solution Explorer).
- Write code in the Code Editor.
- Click the Run button or press the F5 key to run the program. A command Prompt window will appear (commands Prompt windows) to display Hello world.
You can also use the command line instead of the Visual Studio IDE to compile C # programs:
- 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 the ENTER key to compile the code.
- If the code has no errors, the command prompt goes to the next line and generates the helloworld.exe executable file.
- Next, type HelloWorld to execute the program.
- You will see "Hello World" printed on the screen.
C # Program Structure