Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The structure of the C# program


May 11, 2021 C#


Table of contents


The structure of the C#program

In this section, we'll learn the structure of the programming language, and in order for everyone to have a better understanding of the structure of the C#program, we'll first demonstrate a minimal, simplest of the structure of the C#program as a reference for the next chapter.

An instance of the C# Hello World

A C#program consists mainly of the following sections:

  • Namespace Declaration
  • A class
  • Class method
  • Class property
  • A Main method
  • Statements and Expressions
  • Comments

Let's look at a simple code that prints "Hello World":

using System;
namespace HelloWorldApplication
{
   class HelloWorld
   {
      static void Main(string[] args)
      {
         /* 我的第一个 C# 程序*/
         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 program above:

  • The first line of the program using System; /b10>- The using keyword is used to include the System namespace in the program. A program typically has more than one using statement.
  • The next line is the namespace declaration. A namespace is a series of classes. The HelloWorldApplication namespace contains the class HelloWorld.
  • The next line is the class declaration. T he class HelloWorld contains the data and method declarations used by the program. C lasses typically contain multiple methods. M ethod defines the behavior of the class. Here, the HelloWorld class has only one Main method.
  • The next line defines the Main method, which is the entry point for all C# programs. The Main method explains what the class will do when executed.
  • The next line , . . . .
  • The Main method passes through the statement Console.WriteLine ("Hello World"); /b10>Specifies its behavior.

    WriteLine is a method that defines the Console class in the System namespace. The statement displays the message "Hello, World!" on the screen.

  • The last line console.ReadKey (); /b10>is for VS.NET users. This causes the program to wait for a keystroke to move, preventing the screen from running quickly and shutting down when it starts from Visual Studio .NET.

The following points are worth noting:

  • The case is case sensitive.
  • All statements and expressions must be given a sign (; The end.
  • The execution of the program starts with the Main method.
  • Unlike Java, file names can be different from class names.

Compile and execute the C# program

If you'Studio.Net visual to compile and execute a C# program, follow these steps:

  • Start Visual Studio.
  • On the menu bar, select File -gt; New -project.
  • Select Visual C#from the template, and then select Windows.
  • Select Application Console.
  • Make a name for your project and click the OK button.
  • New projects appear in 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 appears that shows Hello World.

You can also use the command line instead of the Visual Studio IDE to compile the program:

  • Open a text editor and add the code mentioned above.
  • Save the file as helloworld .cs.
  • Open the command prompt tool to navigate to the directory where the file was saved.
  • Type csc helloworld .cs enter to compile the code.
  • If the code is error-free, the command prompt goes to the next line and generates a helloworld .exe executable file.
  • Next, type helloworld to execute the program.
  • You'll see Hello World printed on the screen.

If you are prompted not to recognize the csc command, configure the environment variable (Window10) as follows:

Right-click the Computer icon on the desktop and click on "Properties" - "Advanced System Settings" - "Environment Variables" - "System Variables" in the pop-up menu, find the variable Path, add the path "C:?Windows\Microsoft.NET\Framework\v2.0.50727" (note that multiple paths are separated by ;), and other versions of Windows are available later).

The structure of the C# program