The best C # program structure Tutorial In 2024, In this tutorial you can learn
C # Hello World Examples,Compile & execute C # program,
C # program structure
Before we learn the basic building blocks of C # programming language, let us look at a minimum of C # program structure, to serve as reference for the next chapter.
A C # program includes the following components:
- Namespace declaration (Namespace declaration)
- A class
- Class Methods
- Class Properties
- A Main method
- Statements (Statements) & Expressions (Expressions)
- Note
Let's look at a can print out "Hello World" simple 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 results:
Hello World
Let's look at each part of the above program:
The following points are noteworthy:
- C # is case-sensitive.
- All statements and expressions must be a semicolon (;) at the end.
- Start program execution from Main method.
- Unlike Java, the file name can be different from the name of the class.
If you are using Visual Studio.Net C # compile and execute the program, please follow the steps below:
- Start Visual Studio.
- On the menu bar, select File -> New -> Project.
- Select Visual C # from the template, and then select Windows.
- Select Console Application.
- To develop a name for your project, and then click the OK button.
- The new project appears in Solution Explorer (Solution Explorer) in.
- Write code in the code editor (Code Editor) in.
- Click the Run button or press the F5 key to run the program. There will be a command prompt window (Command Prompt window), displays Hello World.
You can also use the command line instead of the Visual Studio IDE to compile a C # program:
- Open a text editor, add the code mentioned above.
- Save the file ashelloworld.cs.
- Open a command prompt tool, navigate to the directory to save the file.
- Typecsc helloworld.cs and press the enter key to compile the code.
- If the code is not an error, the command prompt enter the next line, and generateshelloworld.exe executable file.
- Next, typehelloworld to execute the program.
- You will see "Hello World" printed on the screen.