C # Tutorial C # program Structure

Source: Internet
Author: User

C # Program Structure

Before we learn the basic building blocks of the C # programming language, let's take a look at the minimal program structure of C # for reference in the next section.

C # Hello World instance

A C # program mainly includes the following sections:

namespace declaration (Namespace declaration)

A class

Class method

Class Property

A Main method

Statements (statements) & Expressions (Expressions)

Comments

Let's look at a simple code that can print out "Hello World":

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 first line of the program using System; -The Using keyword is used to include the System namespace in your program. A program generally has multiple using statements.

The next line is the namespace declaration. A namespace is a series of classes. The Helloworldapplication namespace contains class HelloWorld.

The next line is the class declaration. Class HelloWorld contains the data and method declarations used by the program. Class generally contains multiple methods. Method 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 describes what actions the class will do when it executes.

The next line of/*...*/will be ignored by the compiler, and it will add additional annotations to the program.

The Main method passes the statement Console.WriteLine ("Hello World"); Specifies the behavior of it.

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

Last line Console.readkey (); is for vs.net users. This allows the program to wait for a keystroke action to prevent the screen from running and shutting down quickly when the program starts 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 with the Main method.

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

Compiling & executing C # programs

If you are using Visual Studio.NET to compile and execute a C # program, follow these steps:

Start Visual Studio.

On the menu bar, select Project, File, New.

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

Select Console Application.

Make a name for your project, and then click the OK button.

The new project appears in Solution Explorer (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 (commands Prompt windows) to display Hello world.

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

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 CSC HelloWorld.cs and press the ENTER key to compile the code.

If the code has no errors, the command prompt goes to the next line and generates the Helloworld.exe executable file.

Next, type HelloWorld to execute the program.

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

The above is the "C # Tutorial" C # Program structure of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.