"Tutorial" "Reprint" C # Sharp Experience (Li Jianzhong)-01

Source: Internet
Author: User
Tags definition command line exception handling execution header implement mscorlib win32
Tutorial C # Sharp Experience
Nanjing University of Posts and Telecommunications Li Jianzhong (cornyfield@263.net)

The C # language is a simple, modern, elegant, object-oriented, type-safe, platform-independent new component programming language. Its grammatical style derives from the C/s + + family, combines Visual Basic with high efficiency and powerful, and is the mainstream language of Microsoft's Microsoft.NET platform built to lay its next internet hegemony. It was launched by its powerful operational capabilities, elegant grammar style, innovative language features, first, such as component-oriented programming support and well received by programmers around the world praise and love. "It's the computer language I've been dreaming of for years!" "-many senior programmers are surprised to get C #. From the C # language name (c Sharp) We can also see Microsoft's ambition to build its next-generation Internet depth service. The C # language has now been submitted by Microsoft to the European Computer Manufacturers Association (ECMA), and a standardized C # will enable any vendor to implement its development tools and supporting software on any platform, which provides a powerful driving force for the development of C #, and we can see Microsoft's unprecedented vision and wisdom here. Component programming has become a consistent choice for the next Generation program development in the World software industry, and it is the deep development of object-oriented programming in the 90 's. C # when it comes to life, the best of all time, "first-level component-oriented programming support" is by no means easy to say. In fact, component features have been deeply embedded in the C # language at all levels and are a sharp (Sharp) for C #. In the following article, I will be from the C # language to show the various aspects of the C # language of the ubiquitous component features, in-depth elaboration of C # component-oriented programming. The whole topic is divided into ten lectures: "The first lecture ' hello,world! "Program", "second, Introduction to C # language Basics", "The third part is about the Microsoft.NET platform infrastructure", "" class structure and Enumeration "," chapter five constructors and Destructors "," sixth domain method properties and indexers "," seventh speaks interface Inheritance and polymorphism "," eighth delegate and event "," Nine-talk array with string "," tenth speaking features and mappings "," 11th speak COM Interop unmanaged programming with exception handling "," 12th lecture on C # weaving future--c# Programming Model Overview ". The first lecture "hello,world! "Program" Hello world! " Program is a programmer has always been a romantic agreement, but also a great dream-one day, from the human hand of the computer will face this beautiful world to say "Hello world!." It's a good starting point for learning a new language, so let's start with the following examples:
HelloWorld.cs by cornfield,2001
CSC HelloWorld.cs
Using System;
Class HelloWorld
{
public static void Main ()
{
Console.WriteLine ("Hello World!");
}
}
We can write this code by turning on the simple Notepad program that comes with Windows--the author recommends a fairly straightforward editing tool that is very simple to expose the program code at first. We save its filename 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. We type HelloWorld to execute this file to get the following output: Hello world! Below we will carefully analyze the above code and the entire program's compilation output and execution process. Look at the two lines of code starting with the file, which is a single line comment statement for the C # language. Similar to the C + + language, C # supports two annotation methods: a single-line comment starting with "//" and a multiline comment paired with "/*", "* *". cannot be nested between annotations. Let's look at the following "Using System;" statement, which is the using namespace indicator of the C # language, where "system" is the class library provided by the Microsoft.NET system. The C # language does not have its own language class library, which directly acquires the Microsoft.NET System class library. The Microsoft.NET class Library provides a very powerful universal feature for our programming. This statement allows us to replace the type "System.Console" with a short alias "Console". Of course, the using indicator is not necessary, and we can get the type with the global name of the type. In fact, using a USE statement does not have any effect on the program that the C # compiles output, it is simply a type reference that simplifies the longer namespace. We then declare and implement a HelloWorld class that contains a static main () function. C # All declarations and implementations are placed in the same file, unlike C + +, which separates the two. The Main () function is very special in C #, which is the entry point for all executable programs specified by the compiler. Because of its specificity, we have several guidelines for the main () function: the main () function must be encapsulated in a class or struct to provide an entry point for an executable program. C # has a full object-oriented programming approach, and C # can not have global functions like C + +. The Main () function must be a static function. This allows C # to run programs without creating instance objects. The Main () function protection level has no special requirements, public,protected,private, and so on, but generally we all specify it as public. The first letter of the Main () function name is capitalized, otherwise it will not have the semantics of the entry point. C # is a case sensitive language. The parameters of the Main () function are onlyTwo parameter forms: a command-line argument that has no parameters and a string array, that is, static void main () or static void Main (String[]args), which accepts command-line arguments. There can be only one entry point for a main () function in a C # program. Other forms of parameters do not have entry point semantics, and C # does not recommend overloading the main () function with other parameter forms, which can cause compilation warnings. The Main () function return value can only be void (no type) or int (integer type). Other forms of return values do not have entry-point semantics. Let's look at the internal implementation of the main () function in the "HelloWorld.cs" program. As mentioned earlier, the console is a class under the namespace system, which represents the console we normally deal with. And here we are calling 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 language class library, which directly acquires the Microsoft.NET System class library. We do this by getting the System.Console.WriteLine () in the Microsoft.NET System class library to do the console output we want. So we're done with "Hello world!." Program. But it's not that simple! While we compile the output to execute the program, at the same time, many mechanism of Microsoft.NET bottom is surging secretly, want to experience the sharp of C #, we have no reason to ignore its back Microsoft.NET platform. In fact, if there is no microsoft.net platform, it is difficult to say what C # has the sharp point. Let's take a look at what happens when we compile the "HelloWorld.cs" file with the csc.exe command. Yes, we got the HelloWorld.exe file. But that's just the way things are, in fact that HelloWorld.exe is not an executable file at all! Then what is it? And why can it be implemented? OK, here's where to answer these questions. First, the HelloWorld.exe of the compiled output is an intermediate language (IL), metadata (Metadata) and an additional standard executable header for the target platform added by the compiler (such as the WIN32 platform is a standard Win32 executable header) consisting of a PE (portable executable, portable executable) file, Rather than a traditional binary executable-although they have the same extension. The intermediate language is a set of instruction sets that are independent of the CPU and can beThe Just-in-time compiler jitter The native code of the target platform. The intermediate language code makes the high-level language of all microsoft.net platforms C#,VB.NET,VC. NET can be platform independent, and interoperability between languages. Metadata is a collection of tables embedded in a PE file. Metadata describes the information that some common language runtime (Common Language Runtime) needs to know when code is executed, such as data types in code. Meta data makes. NET application code is self-describing and provides type safety, which previously required an additional type library or interface definition language (Interface definition Language, referred to as IDL). This explanation may still be a bit confusing, so let's actually dissect the PE file. The tool we use is the ildasm.exe from the. NET SDK Beta2, which helps us extract the relevant data from the PE file. We type the command "ildasm/output:helloworld.il HelloWorld.exe" and generally get two output files: helloworld.il and Helloworld.res. Where the latter is extracted from the resource file, let us disregard the helloworld.il file. We can use Notepad to see the metadata and intermediate language (IL) code, due to the length of the relationship, we will only extract the intermediate language code listed below, about the metadata table items we do not talk about: Class private auto ANSI BeforeFieldInit HelloWorld
extends [mscorlib]system.object
{
. method public hidebysig static void Main () cil managed
{
. entrypoint
Code size (0XB)
. maxstack 8
Il_0000:ldstr "Hello World!"
Il_0005:call void [Mscorlib]system.console::writeline (String)
Il_000a:ret
}//End of method Helloworld::main

. method public Hidebysig specialname rtspecialname
instance void. ctor () CIL managed
{
Code size 7 (0x7)
. maxstack 8
il_0000:ldarg.0
Il_0001:call instance void [Mscorlib]system.object::.ctor ()
Il_0006:ret
}//End of method Helloworld::.ctor

}//End of Class HelloWorld

Our rough feeling is that it is similar to earlier assembly language, but it has the function of object definition and manipulation. We 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, and we don't have a constructor defined in the "HelloWorld.cs" source--yes, we didn't define the constructor, but C # 's compiler added it for us. You can also see that the C # compiler also forces the HelloWorld class to inherit the System.Object class, although we have not specified it. We will analyze these advanced topics in future lectures. So how does the PE file execute? The following is a typical c#/. NET application: The application (PE file) that the user executes the compiler output, the operating system loads the PE file, and the other DLLs (. NET Dynamic Connection library). The operating system loader jumps to the entry point of the program based on the header of the executable file in the front PE file. Obviously, the operating system does not perform intermediate languages, and the entry point is also designed to jump to the _ Corexemain () function portal of the Mscoree.dll (. NET platform's core support DLLs). The Corexemain () function begins executing the intermediate language code in the PE file. The execution here means that the common language runtime, in accordance with the object method called, uses the Just-in-time compiler to compile the intermediate language into the native binary code, execute and store the machine cache as needed. During the execution of the program, the garbage collector is responsible for the memory allocation, release and other management functions.
When the program is finished, the operating system unloads the application.
Clear knowledge the execution of the PE file for the compiled output is the key to mastering the C # language programming, which in itself interprets the high-level kernel mechanism of the C # language and the secretive nature of the Microsoft.NET platform behind it. A "Hello world!" The generalization of the program is enough, after we have a good starting point for the C # language, the following topics will be with you to enjoy the C # Basic language, spy on the Microsoft.NET platform construction, step-by-step experience of C # Sharp programming bliss, let ' s go!

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.