C # program structure
Before we learn the basic building blocks of C # programming language, let us look at a minimum of C # program structure, to serve as reference for the next chapter.
C # Hello World Examples
A C # program includes the following components:
- Namespace declaration (Namespace declaration)
- A class
- Class Methods
- Class Properties
- A Main method
- Statements (Statements) & Expressions (Expressions)
- Note
Let's look at a can print out "Hello World" simple 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 look at each part of the above program:
- The first line of the programusing System; - usingkeywords used to contain theSystemnamespace in the program.Using a procedure generally have multiple statements.
- The next line is anamespace declaration.Anamespace is a series of classes.HelloWorldApplicationnamespace contains classesHelloWorld.
- The next line is the class declaration.HelloWorldclass contains data declarations and method used by the program. Class typically contain multiple methods. The method defines a class act.Here,HelloWorld class has only one Main method.
- The next line defines theMain method is that all C # program entry point.Main method Description When performing class action will do.
- /*/en.*/ Next line will be ignored by the compiler, and it will add extracomments in the program.
- Main method statementConsole.WriteLine ( "Hello World"); specifies its behavior.
WriteLineis a methodof the Consoleclass in theSystemnamespace definition. This statement will be displayed on the screen message "Hello, World!".
- The last lineConsole.ReadKey (); for VS.NET users.This makes the program will wait for a key operation, when prevented from Visual Studio .NET startup screen and quickly run off.
The following points are noteworthy:
- C # is case-sensitive.
- All statements and expressions must be a semicolon (;) at the end.
- Start program execution from Main method.
- Unlike Java, the file name can be different from the name of the class.
Compile & execute C # program
If you are using Visual Studio.Net C # compile and execute the program, please follow the steps below:
- Start Visual Studio.
- On the menu bar, select File -> New -> Project.
- Select Visual C # from the template, and then select Windows.
- Select Console Application.
- To develop a name for your project, and then click the OK button.
- The new project appears in Solution Explorer (Solution Explorer) in.
- Write code in the code editor (Code Editor) in.
- Click the Run button or press the F5 key to run the program. There will be a command prompt window (Command Prompt window), displays Hello World.
You can also use the command line instead of the Visual Studio IDE to compile a C # program:
- Open a text editor, add the code mentioned above.
- Save the file ashelloworld.cs.
- Open a command prompt tool, navigate to the directory to save the file.
- Typecsc helloworld.cs and press the enter key to compile the code.
- If the code is not an error, the command prompt enter the next line, and generateshelloworld.exe executable file.
- Next, typehelloworld to execute the program.
- You will see "Hello World" printed on the screen.