C #, full contact in simple and comprehensible (II.)

Source: Internet
Author: User
Tags visual studio
V. C # Editor
Writing a C # program can be done in a text editor or in Visual Studio in the integrated development environment. There are also some third-party editors on the market, some of which are free. Please see here for more information.
Vi. The program structure of C #
A C # program contains a class that has at least one public static method Main, which initializes the program and terminates the program. Create child objects, execute methods, and implement logical processing of software in the Main method. The following is a typical miniature C # routine:
Using System;
Class Myfirstapp
{
static int Main (string[] args)
{
System.Console.WriteLine ("Hello. NET");
return 1;
}
}
In C #, you use the following declaration to introduce an external definition, instead of using #include like C + +:
Using System;
Using System.Data;
The code is then compiled using the C # compiler csc.exe. Assume that the preceding code is saved as a file Hello.cs, using the following command:
csc Hello.cs
The result generates Hello.exe, which writes the information "Hello. NET" to the console Output window.
Although the compiled results file contains the. exe suffix, hello.exe is not a real, unambiguous CPU code snippet. In fact, it contains a. NET byte code. When Hello.exe is started, the CLR extracts important metadata from the compiler to write code. Next, a module called the Just-In-Time compiler maps the code to a specific CPU and starts the actual execution process.
Vii. C # and namespaces
The actual C # program typically contains multiple files, each of which can contain one or more namespaces. A namespace is a name that describes software entities such as classes, interfaces, enumerations, and embedded namespaces to the compiler. namespace and data type must have a unique name. In a C # program, it can be identified by the full qualification name of an element, which indicates a hierarchical relationship. For example, System.String is a complete qualification name for a. NET string type. However, to simplify the code, just declare that the System namespace is being used:
Using System;
You can use a relative name, such as String, as a synonym for the full name, and still represent System.String at the end.
By using the NAMESAPCE keyword, we can also wrap a C # program or class in its own namespace, such as:
Namespace Myown
{
Using System; For String
Class Myfirstapp
{
static int Main (string[] args)
{
System.Console.WriteLine ("Hello. NET");
return 1;
}
}
}
The namespace Myown is part of the global namespace. Calling it does not require the use of a prefix because its full qualification name is simply Myown. Defining a namespace is one way to preserve the uniqueness of a common name. In fact, if the names of two classes conflict, but as long as they belong to a different namespace, two classes are still unique.
Compilation and compiling of a classical routine in C #
1, write code
"Hello World" is almost the first routine to learn about any programming language. Let's do the work with C # below. In any of the C # editors mentioned above (such as WordPad), type the following code:
Using System;class MyClass {static void Main () {Console.WriteLine ("Hello world!");}

Then save it as a file MyClass.cs.
2, compile the program
Note: The C # compiler requires at least one argument, such as a filename. Assuming your C # file name is MyClass.cs, now use the command-line program csc.exe to compile the MyClass.cs file above:
CSC MyClass.cs
The C # compiler then generates a Myclass.exe file in the bin directory of the engineering file. Run this EXE to see what the output is.
3, the meaning of the Code
Let's look at the meaning of the code line by row:
The first line of the program is the using System. Why use System? Because system is the namespace of the storage System class, the console (console) class used by the program to display output on the console is defined in the System namespace.
The next line is Class MyClass. The class keyword in C # is used to create a new class. Each class has a static void Main () function, which is the entry point of a C # program.
The WriteLine method of the console class is responsible for outputting text information to the console.


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.