Introduction to the. NET Framework

Source: Internet
Author: User

Beginner. NET will certainly have a series of questions, such as (the following for their own questions):

1) what is. NET Framework, what does it contain?

2) What is an assembly and how does it run in the CLR (Common language runtime)?

3) C # and VB.net belong to the. NET platform, the fundamental connection and difference between them, and why the assemblies between them can call each other (if creating a new type of orientation. NET language, to follow what)?

To understand the above questions, you need to figure out the concepts of CIL (common Intermediate language), the CLR (Common language Runtime), the CTS (common type System), the CLS (Common Language Specification), and so on, followed by a brief summary of some of the others ' articles.


first of all, a popular understanding. NET platform,. The concept of the NET Framework (framework)

"Platform" (in this case, the software technology platform, which refers to this) is the ability to operate independently and autonomously, providing the environment in which the upper systems and applications supported by the operation depend. Extract is, the platform is an environment.

My popular understanding. NET platform is an intermediary between. NET applications and the operating system, first it provides an environment for. NET applications to run, and secondly it acts as a "decoupling" between. NET applications and the operating system, making the application on top of the platform independent of the operating system (the machine instruction set). As for how to decouple, look at its compilation process, which will be explained below.


  


The framework is like a semi-finished product of an application, a reusable set of components that have been developed by the predecessors, for you to choose from and then add blood and meat to complete your own system. Its ideas and design patterns are somewhat similar, the framework is code reuse, design patterns are design reuse. The framework also seems to provide a set of specifications, it regulates the development and deployment of application systems, the well-known Java framework is defined by the 13 specifications. Similarly, the NET framework provides a number of specifications, which are described below.


  


Roughly speaking, one of them. NET application is an application that runs on top of the. NET Framework. Or, A. NET application is an application that is written using the. NET Framework class library and runs on top of the common language runtime CLR (Common Language Runtime). Here's how to start solving the above questions:


assemblies and their operations

First, two concepts cil--universal Intermediate Language, clr--universal language Runtime two concepts (the specific concept of Baidu encyclopedia).

We know that ordinary non-platform applications (such as VB applications) are precompiled, compiled, compiled, linked a few steps, the resulting executable file already contains the code of the local processor, support it to run the operating system and the local machine instruction set.

Under the. NET Framework, the high-level language (example C #) is compiled to produce a result file called an assembly , followed by a. dll (class library) or. exe (executable, console application compilation result). And the assembly is not binary machine code, is not directly run, need to go through the CLR (Universal Language Runtime) of the immediate compilation to generate the machine code recognized by the operating system.

Below we write two identical console applications in C # and vb.net, each defining a string "Hello World" and then outputting:

C#:


  


Vb. NET:


  


Then compiled to generate the corresponding assembly. exe file, below we use the VS own anti-compilation tool-il DASM (directory: Start->vs2010-->windows SDK Tools->il The disassembler) opens the assembly generated above, respectively.

C # Assemblies:


  


Vb. NET assembly:


  


Comparing the two assembly files, you can see that the code for both is almost identical.

The assembly-like code in the file opened with Il dasm above is the cil-generic intermediate language. You can see that vb.net and C #, the compiled assembly is the same format, and the CIL code that the assembly contains is similar when the functionality implemented by the program is the same. This can be done by:


  


The above mentioned assembly (CIL) is not the local machine language that the CPU can execute directly. This language is also needed. NET runtime (CLR) environment, a two-time compilation process known as immediate compilation before execution can be turned into an instruction that the computer can recognize.


  


CIL is also a programming language, a kind of intermediate code language that is lower than C # and higher than the machine code, similar to the. class file in Java. From the front, CIL is a stack-based language, and it provides many object-oriented language features such as class, interface, inheritance, polymorphism, and so on, so it is a fully object-oriented language. If you wish, you can even write CIL code directly and compile it using the CIL compiler il ASM (il assembler,il assembler). Just like most low-level languages, this approach can make development less efficient.

C # Source programs are independent of C # when compiled into assemblies, so assemblies can be called by other kinds of languages, and because the assembly does not contain instructions for the local machine, it is separated from the specific machine type and can be installed. NET Framework to run any machine.


the fundamental connection and difference between C # and VB.net

(Most of the books or blog posts are quoted below)

The two concepts of cts--public type system and cls--Common Language specification are introduced here.

Imagine how we develop a new language similar to C # or vb.net (after compiling CIL code that can be run in a. NET environment)?

The new language to be developed is equivalent to the high-level language version of CIL, so what actually needs to be done is not determined by the new language, but by CIL. Therefore, a set of CIL definitions, rules, or standards is required. This set of rules defines what our language can do, what it cannot do, and what features it has. This set of rules is called the CTS (Common type System, public type systems). Any high-level language that satisfies this set of rules can be called orientation. The language of the NET Framework. C # and VB.net are just Microsoft's own set of CTS-compliant language, in fact there are many organizations or groups, but also developed such a language, such as Delphi.NET, Fortran and so on.

The CTS defines the types of data that can be defined in the language, the level of access such as private, public, Family (protected in C #), Assembly (internal in C #), Family and Assembly (C # No implementation is provided in, Family or assembly (protected internal in C #).

The CTS also defines constraints, for example, all types implicitly inherit from the System.Object type, and all types can inherit from only one base class. From the name of the CTS and the common type system, it can be seen that not only the C # language satisfies these constraints, all oriented. NET language all need to meet these constraints

As mentioned above, C # does not provide an implementation of family and assembly, and there is no global method in C #. In other words, C # only implements part of the CTS functionality. In other words, the CTS regulates all the capabilities that languages can achieve, but the specific language implementations that conform to the CTS specification do not necessarily implement all the functions defined by the CTS specification.

Obviously, because CIL is the language that the. NET runtime understands, it implements the full functionality of the CTS. Although it is a low-level language, in fact, it has a more complete function. The C # language and CIL relationships can be expressed as follows:

  


  

Now that you understand that the CTS is a set of rules for a language, you can develop a set of languages to conform to the CTS. Assuming that the language is called b#, it implements a very limited CTS, only a few of which are implemented, and its relationship to the CTS and C # languages might look like this:


  


So now there's a problem: is the assembly written by C # able to refer to an assembly written by b#? The answer is obviously no. While C # and b# belong to the CTS, they have nothing in common. Thus, while a separate b# or C # program can be perfectly in the. NET Framework, but they cannot refer to each other.

If the b# project expects a project of another language type to be able to reference it, it requires that the types and features exposed in the b# meet the features of the C # language, i.e. they need to have something in common. The nonpublic portions of b# (private, internal, protected) are unaffected and can be used with unique language features, because these nonpublic portions would otherwise not allow external access. Therefore, if b# wants to be understood and referenced by C #, the part it exposes is going to satisfy some of C # 's specifications, and its relationship with the CTS and C # language will become as follows:


  


If only C # and n# two languages are available in the world, extract their common language features, and then require all exposed types to satisfy these language features, so that C # and n# assemblies can reference each other. The problem is that there are hundreds of different language types, and. NET is designed to implement an open platform that can be run in a simple modification of the existing language. NET Framework, the subsequent development of the new language can also, and the new language does not exist at this time, how to extract its language characteristics? Therefore, a set of norms and standards is needed to define some common language features that most languages share.

For the new language of the future, it can be used by assemblies in other languages as long as it exposes parts that satisfy these specifications. This specification is called CLS (Common Language Specification, Common Language Specification). It is clear that the CLS is a subset of the CTS. Then the relationship between VB.net, C #, and b# can be expressed as:


  


If the public part of an assembly developed with C # takes only the attributes in the CLS, the assembly is called a CLS-compatible assembly (CLSCompliant assembly). Obviously, for the above mentioned FCL Framework class library, where the types are CLS-compliant, only the very individual types of members are not CLS-compliant, which guarantees all orientation. NET can use the types in the Framework class library.

To satisfy the CLS is to require language features to be consistent, what is a language feature? Here are a few specific language features: whether case-sensitive, how the naming rules of identifiers are, what types of primitives can be used, how constructors are called (whether the base class constructor is called), which access modifiers are supported, and so on.

So how do we verify that the assembly is CLS-compliant? NET provides us with a feature clscompliant that makes it easy to check whether an assembly is CLS-compliant at compile time. Let's take a look at the following example:


  


It can be noted that the Assembly was preceded by a CLSCompliant attribute in front of the Clstest class, indicating that the assembly is CLS-compliant. However, three places do not satisfy this requirement, so the compiler gives a warning message. These three places are:

The member cannot be distinguished by casing, so the field name and Method name () are not CLS-compliant.

The return type and parameter type of the method must be CLS-compliant, and the uint and sbyte types are not CLS-compliant, so the GetValue () and SetValue () methods are not CLS-compliant.

The name of the identifier cannot begin with the underscore "_", so the attribute _myproperty is not CLS-compliant.

You will also notice that the compiler gives only warning messages, not error messages, so you can ignore compiler warnings, but this assembly is only used by assemblies written in other C # languages.


Summary

We're right. NET Framework is the first feeling that. NET Framework, and the C # language used to write code, is actually much more than that. It also contains a lot of content, such as the CLI, CIL, CTS, CLS, CLR, JIT, BCL, FCL, etc., which are in the. Net of the beauty of this book are all carried out in a simple explanation, for beginners very practical. If you want to really understand. NET, just coding is far from enough, above these things is fundamental.

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.