A colleague was recommended to use "optional parameters" a few days ago. After the recommendation, I also described it as a new feature in. Net 4. However, it was discovered later that this new feature is the language feature of C #4.0 and has nothing to do with. Net 4. In fact, more than once, I often confuse languages, frameworks, runtime, and sometimes development tools. So today I will summarize the characteristics of several languages that I am interested in C.
1. Optional parameters
The optional parameter is a new feature in C #4.0. Its function is to give the parameter a default value when the caller does not provide the parameter value, which is used as follows:
Static void Main (string [] args) {TestMethod (); TestMethod (10); Console. readLine ();} public static void TestMethod (int parameter = 5) {Console. writeLine (parameter );}
The above code outputs 5 when TestMethod is called for the first time, and 10 for the second time, that is, when no parameter value is provided for TestMethod, 5 is automatically used as the parameter value.
The implementation of this feature depends on the OptionalAttribute and DefaultParameterValueAttribute attributes. That is to say, the TestMethod method can be declared as follows:
Public static void TestMethod ([Optional, DefaultParameterValue (5)] int parameter)
{
Console. WriteLine (parameter );
} The running results are the same.
OptionalAttribute and DefaultParameterValueAttribute are in. net 1.1 and. net 2.0 introduced, that is to say, N years ago, everyone can write methods with "optional parameters", but it is not as good as it is now.
If you have installed. net Framework 4.0 (that is, with the new C # compiler included), you can write the above Code and specify the target framework. net Framework 2.0.. Net Framework 4.
Summary: The mistake I made on this issue is to confuse the language with the Framework. The fourth version of C # language is released together with the fourth version of. Net Framework, so I naturally think that the new features in C #4.0 have something to do with. Net Framework 4. In fact, as long as Microsoft people are willing to, they can be in. net Framework 2.0 and. net Framework 4.0 released any time before the release of a CTP compiler to achieve this language feature, just as Asyn CTP, which was released recently.
2. var keyword
The var keyword is introduced in C #3.0. It does not need to specify a specific type when declaring a local variable. It is used as follows:
Var str = "hello ";
Console. WriteLine (str );
The result is exactly the same as replacing var with string.
This language feature seems to be a weakness. In fact, its advantage is to receive the return value of the LinQ statement, such as Enumerable. the return values of some reloads of GroupBy are IEnumerable>. If you need to write such a long string of code every time you use group, the pleasure of playing with LinQ must be reduced a lot.
The implementation of this language feature is simpler than the optional parameters without the support of the framework. It is completely "collusion" between the language specification and the compiler ". During compilation, the compiler deduce the actual type based on the value assignment statement, and the compiled IL has no var at all.
3. Generic
Generics are new features in C #2.0 and new features in. Net 2.0. No error. It can be said that it is a new feature of. Net, but it is only. Net rather than. Net Framework.
The implementation of generics at the C # language level requires the support of CLR. It can be said that it is a first-class citizen in the. Net world, and IL even modified its syntax for it.
For example, the following type declaration:
Public class TestClass
After compiling the file into IL:
. Class public auto ansi beforefieldinit TestClass
Extends [mscorlib] System. Object visible IL adds the use of angle brackets.
The above three language features are typical. The var keyword is purely sweet at the language level. It doesn't matter which version of CLR or. Net Framework is used if the var compiler can be understood.
Optional parameters require the compiler to understand the value assignment statement after the parameter name, and also support the attribute provided in. Net Framework.
Generic expressions must be supported by CLR and compiler.
If a table is listed, it is as follows:
Supported by language features
Var keyword CLR () Framework () compiler ()
Optional parameter CLR () Framework () compiler ()
Generic CLR () Framework () compiler ()
The compiler for each item in the above table is hooked. Obviously, every syntax-level feature requires the support of the compiler. Otherwise, the language specification is just empty. Here, we will list it as comprehensive.
After writing it, I read it again, and I think it is very boring. Microsoft always releases development tools, framework class libraries, runtime and new version of the compiler, so we are always passive and cannot tell the difference between them. In fact, sometimes we can differentiate these things for observation, which is more conducive to explaining some confusions.
Of course, there are still too few items listed in the final table. I hope you can add them.