1.3. C# program structure

发布时间 : 2025-10-25 13:34:35 UTC      

Page Views: 9 views

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.

1.3.1. C# Hello World instance #

A C#program mainly includes the following parts:

  • Namespace declaration

  • One class

  • Class method

  • Class attribute

  • One Main method

  • Statements & Expressions

  • Annotation

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”:

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(); } } } 

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 multiple using statements.

  • The next line is namespace statement. One namespace 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 class Main 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 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.

  • Last line 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.

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.

1.3.3. 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.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。