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. A C#program mainly includes the following parts: Namespace declaration One One Statements Annotation The suffix of the C# file is The following creates a When the above code is compiled and executed, it produces the following results: 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 The next line is The next line is The next line defines Next line / … / will be ignored by the compiler, and it will add additional comments to the program. Last line The following points are worth noting: C# is case sensitive. All statements and expressions must end with a semicolon (;). The program is executed from Unlike Java, the file name can be different from the name of the class. If you use the 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 Open the command prompt tool and navigate to the directory where the file is saved. Type in If there are no errors in the code, the command prompt goes to the next line and generates Next, type You will see “Hello World” printed on the screen. 1.3.1. C# Hello World instance #
class
Class
method
Class
attribute
Main
method
&
Expressions
.cs
.
test.cs
file that contains simple code that can print out “Hello World”: 1.3.2.
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();
}
}
}
Hello World
System
namespace. A program typically has multiple
using
statements.
namespace
statement. One
namespace
contains a series of classes. The HelloWorldApplication namespace contains the class HelloWorld.
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 class
Main
method.
Main
method is the entry point for all C# programs.
Main
method indicates what the class will do when it is executed.
Main
method through statement
Console.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.
Console.ReadKey()
is aimed at
VS.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.
Main
method begins. 1.3.3. Compile & execute C # programs #
Visual
Studio.Net
to compile and execute the C# program,follow these steps:
helloworld.cs
.
csc
helloworld.cs
and press enter to compile the code.
helloworld.exe
to executable file.
helloworld
to execute the program.