C # Learning Notes (4)

Source: Internet
Author: User
Tags command line contains error handling exception handling garbage collection net command stub visual studio
Notes

Conventions

A typical HelloWorld program written in C #
Using System;
Class HelloWorld
{
public static void Main ()
{
Console.WriteLine ("Hello World!");
}
}

I forgot my first time to say hello to the world in C #, but I'm sure I've already called, and then I used the beta1 version. Now you can go to http://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/msdn-files/027/000/976/ Msdncompositedoc.xml to download the official version of the. NET Framework Software Development Kit (SDK), which includes the. NET Framework mentioned earlier, as well as writing, compiling, testing, developing. Net Everything that the Framework application needs?? Documents, examples, command line tools, and compilers. The C # program can be developed and run after installation, but the general suggestion is to look at the documentation and examples that are in the. Net Framework SDK, and it would be nice if you could write it again.
When I first saw the C # code, I also thought it was like Java, an image of the analogy is: C # and Java is a pair of twins, from a grammatical point of view, their common father of course is not C + + (please note, not VC + +). For a person who has studied the Java language (for example, under), it's so easy to understand this code: the first line of course is commented, and C # supports two annotation methods, a single-line comment with a "//" start and a multiline comment paired with "/*", "* *". The second line (using System) imports the System package (known in C # as the namespace, Namespace), which allows us to easily invoke all classes in the Microsoft.NET base Class library system, in this case using the System namespace The console class, which outputs program run results in the console window. As mentioned earlier, C # does not have a built-in input-output statement, and all the functionality that needs to be implemented comes entirely from. NET base Class library. The function of this sentence is to tell the compiler where to look for the console class to invoke.
Next we declare a class HelloWorld, which has a special method main (), each executable file needs an entry point, in C #, the entry point is the main () method, which is invoked when the program starts. In this method, the console is a class under the namespace system, which represents the console. This calls its static method WriteLine (). Like C + +, static methods allow us to act directly on a class rather than an instance object. The WriteLine () function accepts the string type argument "Hello world!" and sends it to the console display. As mentioned earlier, C # does not have its own class library, which directly acquires the Microsoft.NET System class library. This is where we want to do the console output by getting the System.Console.WriteLine () in the Microsoft.NET System class library. Now use Notepad to write this code and save its file name as HelloWorld.cs, where ". cs" is the extension of the C # source code file. Then type "csc HelloWorld.cs" to compile the file in the command line environment that is configured with the C # compiler. You can see the compile output file HelloWorld.exe. Type HelloWorld to execute this file to get the following output:

Hello World!

This is the first C # program, we use csc.exe to compile it, for this C # compiler, there are the following instructions:
1. It is released free of charge with the. Net Framework SDK and can be invoked on the DOS command line
2. The method used is as follows:
CSC Sourcefile.cs/out:targetfile.exe
If the destination file name is not specified with an output parameter, the default output is the source filename
3. In general, it is within the System folder (Windows or Winnt) under the microsoft.netframeworkv1.0.3705 folder
4. If you install Vs.net, you can activate the Visual Studio.NET Command Prompt window from the Visual Studio.NET Tools project group, which is a command-line environment with the C # compiler configured
5. A compiled C # program using Csc.exe is not a machine code (although it has the suffix name of. exe). As mentioned earlier, C # programs are only compiled into MSIL code.

The compiled file of the C # compiler (csc.exe) is not a strictly executable (it does not contain machine code), but rather a PE (portable executable) file, although it also has the suffix name of. exe. The PE file also contains more than just the intermediate language, in which the package contains metadata (METADATA) and a standard executable header for the target platform added by the compiler.

The intermediate language, exactly, should be called the Microsoft Intermediate Language (Microsoft Intermediate Language,msil), a language defined by Microsoft that is bounded between source code and machine code. In the CLR, it is first packaged in an EXE-formatted pseudocode (P-code) by a specific language compiler. It is then converted to local code execution by a specific compiler. For Microsoft's intermediate language, an image metaphor is: If the CLR is an operating system, then Microsoft's intermediate language is. NET platform of ASM assembly language. It is more advanced than most CPU machine languages, such as it can understand object types and has instructions to create and initialize objects, invoke virtual methods about objects, and manipulate array elements directly. It even has instructions to discover and catch exceptions for error handling.

The metadata (Metadata) and MSIL exist in the compiled program file, describing the definition of the type contained in the program, various types of signatures, and some other data, equivalent to the previous type library (type libraries), as well as other external classes referenced by this program. The primary role of metadata is to provide more information about the code to the CLR. Basically, metadata is used for the following tasks: Information used to represent CLR uses, such as locating and loading classes, instances of these classes in memory, resolving calls, translating IL to Raw code, enforcing security, and setting runtime context boundaries.

A source file written in the C # language executes in a CLR environment: first compiled by the C # compiler into a PE file containing intermediate languages and metadata, when we call the file in the system, The CLR initiates a compiler to convert the MSIL code contained in this PE file into managed local code. This compiler that converts MSIL code to local code is called the JIT compiler (Just in Time,jiter). Please note that it is not the C # compiler we used earlier.
Now let's look at how the JIT compiler works: When the PE file is invoked, the Jie compiler decomposes it into MSIL and metadata, and the MSIL does not directly let. NET invoke the local system interface, but instead specifies the. NET system to compile the CLR DLLs that connect those needs. Compiles a 100% of the local code. The whole process is as follows:
When a type is loaded, the loader creates a stub and joins it with each method of the type. When a method is invoked for the first time, the stub hands control to Jiter. Jiter compiles the MSIL into local code and points the stub pointer to the buffered local code. A method that has been jiter compiled then directly invokes the native code that has been generated, reducing the time that Jiter compiles and executes the code. As you can see, Jiter does not compile all the MSIL into local code at once, but compiles it when we need it, meaning that some of the code may never have been compiled. Obviously the advantage of this is that it ensures the security of the runtime and does not lose too much efficiency.

This is the step of a C # program when it executes. The whole process is like this:
1 compiling source code into intermediate language by C # compiler
2 loading managed code, which includes resolving the memory name, surface class (laying out classes), and creating the stub necessary for JIT compilation. The class loader also enhances security by performing regular checksums, including enforcing some access rules.
3 convert IL to original code with Jiter
4 loading metadata, verifying type safety and the integrity of the method
5) garbage collection (GC) and exception handling
6 description and error-checking services
7 Manage threads and contexts and remote administration.

There is no need to fully understand these concepts, and in later studies will be one of them to realize their wonderful, now you need to do (if you have not done so), is to find Ildasm.exe this file (in general, it will be in the same folder as Csc.exe). As the name suggests, this is an MSIL Disassembler (. Net Framework IL disassembler), which enters ildasm under the Command line window helloworld.exe/out= Helloworld.il will get two files: helloworld.il and Helloworld.res. The former includes decompile metadata and MSIL code, and the latter is the extracted resource file. By opening the helloworld.il file in Notepad, you can see that it defines and implements a HelloWorld class that inherits from System.Object and two functions: Main () and. ctor (). Where. ctor () is the constructor of the HelloWorld class. Metadata and other related information are also included in this file. If this is not intuitive enough, you can type ildasm helloworld.exe in the Command line window so that you can start the ILDasm window and show us the Helloworld.exe file that was recompiled.

If you look at the code a few times, it's not important to understand all of it, but hopefully you'll also see the metadata in the file, which contains information about the assembly and its modules, types, and members (such as methods) that all Runtime and compilers need.

In this way, I would like to talk about learning. As you know, in the environment we are in, learning is always meant to be a painful process, learning a new knowledge seems to be always for their own some kind of demand, I do not think that there is anything wrong, but I always feel that, in addition to get a high salary and respected, learning should also bring us more happiness. Some of the knowledge we may not need now, such as some of the things mentioned earlier, but we know that it is a happy thing.

Wisdom itself is good, one day we will die, the pursuit of wisdom of the road will be people walking. I can't see what's going to happen after I die. But when I was alive, I was happy to think about it. Wang Xiaobo

Today is April 7, 2002, another three days is the anniversary of Wang Xiaobo, do not know how many people will remember this day, but also remember this person. At the end of this article, I recommend the works of wavelets?? Every mature person should read the text of a wavelet. In his essays, silent majority, there is a remark about his side as a programmer:
"I won't sleep tonight without this C + +," he said. ”


>>> not finished, to be continued ...




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.