CTS, CLS, and CLR are basic concepts of the. NET Framework, but they are often ignored by programmers. A clear understanding of the three concepts helps. Net programmers write efficient and secure. net programs.
Knowledge points involved
• Basic concepts of. net
• Relationship between CLs, CTS and common languages
Analyze problems
The full name of CLR is the common language runtime. Readers can understand CLR as a set of engines that contain. net programs and libraries that conform to the basic structure of the common language. CLR is a standard implementation. Almost all. net programs we develop are implemented based on CLR class libraries and run on the running engine provided by CLR. The. NET Framework refers to the public Language Runtime Library.
The common language infrastructure (CLI) is a language and data format specification submitted by Microsoft to ECMA. CLR is the only implementation version of the common language infrastructure so far.
The full name of CTS is the common type system ). The previous article has introduced the concept of the Basic Public language (CLI). The CLI specifications submitted by Microsoft include the Common Type System (CTS) and the common intermediate language (CER) file Format and metadata format at the bottom. A common type system defines a language specification that can run on CLR. Although many languages do not comply with the CTS specification, many languages can compile programs that can run on CLR by enhancing the compiler and changing the language's additional specification.
Compiling a program written in a language can run on the CLR. It does not mean that the language fully complies with the CTS specifications. For example, the C ++ language keeps the section that does not comply with the CTS specification, and compiles the code that does not comply with the CTS into the original code rather than the intermediate code during compilation.
Finally, let's take a look at the concept of Cls. The common language specification (CLS) is a subset of CTS and defines the minimum specification required for the language of the program to be written on the. NET platform. Because. Net allows programs written in different languages to run together, CLS specifications are developed to avoid errors caused by different language features.
To better understand CLs, let's look at this section of C # code:
Code 2-1 CLS specification: Cls. CS
Using system;
Using system. Collections. Generic;
Using system. text;
// Declare that C complies with CLS specifications
[Assembly: clscompliantattribute (true)]
Namespace net. mst. Second
{
Public class CLS
{
Private uint I; // This is a private member, so it does not violate the CLS specification.
Public Cls ()
{
I = 0;
}
Static void main (string [] ARGs)
{
}
// The uint is not defined in CLS because it does not comply with the CLS specification.
Public uint Geti ()
{
Return I;
}
}
}
In code 2-1, the program declares that the CLS type must comply with the CLS Specification through the clscompliantattribute feature. The uint type in C # does not have a corresponding type in CLs, so uint is a language part that does not conform to Cls. At the beginning of the CLS class, the program defines a uint type private member variable I, which does not cause the CLS class to not comply with the CLS rules, because the private variables are only visible to the class. The program then defines a public method to return the uint type, which causes the entire CLS type to not comply with the CLS specification. Compile this code and get the following warning:
C: \ David \ net MST \ chapter \ Cls. CS (23,21): Warning cs3002: return type of 'net. MST. second. cls. geti () 'is not CLS-compliant
Now, the reader has understood the relationship between CLs and CTS.
Answer
The Common Language Runtime Library (CLR) is a CLI implementation that includes the. NET runtime engine and a class library compliant with the CLI.
The general type System (CTS) is included in the CLI specification submitted by Microsoft and defines a type specification that can run on CLR.
The common language specification is a subset of CTS and defines the minimum specification set that all. Net-oriented programs need to comply.
What are CTS, CLS, and CLR?