1. Dynamic ExpandoObject
Familiar with JS friends know that JS can write:
Copy CodeThe code is as follows:
var t = new Object ();
T.ABC = ' something ';
T.value = 243;
Now this JS Dynamic language features, we can also be used in C #, if a variable is declared as the ExpandoObject type. The following example:
Copy CodeThe code is as follows:
static void Main (string[] args)
{
Dynamic T = new ExpandoObject ();
T.ABC = "ABC";
T.value = 10000;
Console.WriteLine ("T ' s ABC = {0},t ' s value = {1}", T.ABC, T.value);
Console.ReadLine ();
}
C # 4.0 has a new namespace system.dynamic to support this application, what is the point of this usage, and now I'm not quite sure, it's also a temptation for C # to transition to a dynamic language.
2. Generic Auto-conversion
Before c#4.0, the following code is not compiled.
Copy CodeThe code is as follows:
ienumerable<object> Objs = new List<string> {
"I ' m 0", "I am 1", "I am 2"
};
However, such declarations are allowed in c#4.0, but are limited to generic interfaces, and similar practices for generic types are not allowed, and the following code is a compilation error
Copy CodeThe code is as follows:
List<object> objlist = new List<string> {
"I am 0", "I am 1", "I am 2"
};
3. Optional parameters of the method parameter
Syntax for declaring the following method
Copy CodeThe code is as follows:
static void dosomething (int notoptionalarg,string arg1 = "Default Arg1", String arg2 = "Default Arg2") {
Console.WriteLine ("arg1 = {0},arg2 = {1}", ARG1,ARG2);
}
This method has three parameters, the first is the required parameter, the second and third are optional, and they all have a default value. This form is useful for several method overloads of fixed parameters.
Call as follows:
Copy CodeThe code is as follows:
static void Main (string[] args)
{
DoSomething (1);
DoSomething (1, "gourd");
DoSomething (1, "gourd", "cucumber");
Console.ReadLine ();
}
Perhaps you would think, if I had a method with an optional parameter method that does not choose a method signature of the same way, C # How to deal with it, we look at the following code
Copy CodeThe code is as follows:
static void dosomething (int notoparg, string arg)
{
Console.WriteLine ("Arg1 = {0}", Arg);
}
I overloaded a dosomething. This method has two parameters, but there are no optional parameters, and the experiment proves that the call
DoSomething (1, "Arg") takes precedence over methods that do not have optional arguments.
4. Named parameters of method parameters
Named parameters let us specify parameter names when invoking a method to assign values to parameters, in which case the order of the parameters can be ignored. The following method declares:
Copy CodeThe code is as follows:
static void dosomething (int height, int width, string openername, string scroll) {
Console.WriteLine ("height = {0},width = {1},openername = {2}, scroll = {3}", Height,width,openername,scroll);
}
We can do this by invoking the method declared above
Copy CodeThe code is as follows:
static void Main (string[] args)
{
DoSomething (scroll: "No", Height:10, Width:5, Openername: "Windowname");
Console.ReadLine ();
}
Obviously, this is a syntactic sugar, but it makes sense in the case of many method parameters, which can increase the readability of the code.
C # 4.0 new features