C# program structure
Before we learn the basic building blocks of the C# programming language, let’s take a look at the smallest program structure of C# for reference in the following chapters.
C# Hello World instance
A C#program mainly includes the following parts:
Namespace declaration
One
class
Class
methodClass
attributeOne
Main
methodStatements
&
ExpressionsAnnotation
The suffix of the C# file is .cs
.
The following creates a test.cs
file that contains simple code that can print out “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 results:
Hello World
Let’s take a look at the various parts of the above program:
The using System;-using keyword on the first line of the program is used to include
System
namespace. A program typically has multipleusing
statements.The next line is
namespace
statement. Onenamespace
contains a series of classes. The HelloWorldApplication namespace contains the class HelloWorld.The next line is
class
statement. The class HelloWorld contains the data and method declarations used by the program. Classes generally contain multiple methods. Method defines the behavior of the class. Here, there is only one HelloWorld classMain
method.The next line defines
Main
method is the entry point for all C# programs.Main
method indicates what the class will do when it is executed.Next line / … / will be ignored by the compiler, and it will add additional comments to the program.
Main
method through statementConsole.WriteLine("Hello World")
specifies its behavior. WriteLine is a method of a Console class defined in the System namespace. The statement displays the message “Hello World” on the screen.Last line
Console.ReadKey()
is aimed atVS.NET
user’s. This causes the program to wait for a keystroke action to prevent the program from``Visual Studio .NET`` screen runs quickly and closes when it starts.
The following points are worth noting:
C# is case sensitive.
All statements and expressions must end with a semicolon (;).
The program is executed from
Main
method begins.Unlike Java, the file name can be different from the name of the class.
Compile & execute C # programs
If you use the Visual Studio.Net
to compile and execute the C# program,follow these steps:
Start Visual Studio.
On the menu bar, select File-> New-> Project.
Select Visual clients from the template, and then select Windows.
Select Console Application.
Make a name for your project and click the OK button.
The new project appears in solution Explorer.
Write code in the Code Editor.
Click the Run button or press F5 to run the program. A command prompt window appears showing Hello World.
You can also use the command line instead of Visual Studio IDE to compile the C# program:
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 in
csc helloworld.cs
and press enter to compile the code.If there are no errors in the code, the command prompt goes to the next line and generates
helloworld.exe
to executable file.Next, type
helloworld
to execute the program.You will see “Hello World” printed on the screen.