C# Tutoriallogin
C# Tutorial
author:php.cn  update time:2022-04-11 14:06:23

C# program structure



Before we learn the basic building blocks of the C# programming language, let us first look at the minimal program structure of C# as a reference for the following chapters.

C# Hello World Example

A C# program mainly includes the following parts:

  • Namespace declaration

  • A class

  • Class method

  • Class attribute

  • A Main method

  • Statements & Expressions

  • Comments

Let’s see one that can be printed 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 look at the various parts of the above program:

  • The first line of the programusing System; - using keyword is used to include System naming in the program space. A program generally has multiple using statements.

  • The next line is the namespace declaration. A namespace is a sequence of classes. The HelloWorldApplication namespace contains the class HelloWorld.

  • The next line is the class declaration. Class HelloWorld contains data and method declarations used by the program. Classes generally contain multiple methods. Methods define the behavior of a 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. Main Method describes what the class will do when executed.

  • The next line /*...*/ will be ignored by the compiler, and it will add an additional comment to the program.

  • The Main method specifies its behavior through the statement Console.WriteLine("Hello World"); .

    WriteLine is a method of the Console class defined in the System namespace. This statement displays the message "Hello, World!" on the screen.

  • The last line Console.ReadKey(); is for VS.NET users. This causes the program to wait for a key press, preventing the screen from quickly running and closing when the program is launched from Visual Studio .NET.

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 from the Main method.

  • Unlike Java, the file name can be different from the name of the class.

Compile & execute C# program

If you use Visual Studio.Net to compile and execute C# program, please follow the steps below:

  • Start Visual Studio.

  • On the menu bar, select File -> New -> Project.

  • Select Visual C# from the template, then select Windows.

  • Select Console Application.

  • Choose a name for your project and click the OK button.

  • The new project will 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 will appear, displaying Hello World.

You can also use the command line instead of Visual Studio IDE to compile C# programs:

  • Open a text editor and add the above code.

  • 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 enter to compile the code.

  • If there are no errors in the code, the command prompt will enter the next line and generate the helloworld.exe executable file.

  • Next, type helloworld to execute the program.

  • You will see "Hello World" printed on the screen.

php.cn