Absrtact: Today we combine code examples to look at the four more important features in c#4.0.
In previous articles, we introduced the history of C # and the new features of C # 4.0, including: dynamic, named and optional parameters, dynamically importing, and covariance and contravariance. Today we combine code examples to look at the four more important features in c#4.0.
1. Dynamic ExpandoObject
Familiar with JS friends know that JS can write:
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:
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, and it's a temptation for C # to transition to a dynamic language.
2. Generic Auto-conversion
Before C # 4.0, the following code was not compiled by the
ienumerable<object> Objs = new List<string> {"I ' m 0", "I am 1", "I am 2"};
In C # 4.0, such declarations are allowed, but are limited to generic interfaces, and similar practices for generic types are not allowed, and the following code is a compilation error
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
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:
static void Main (string[] args)
{
DoSomething (1);
DoSomething (1, "gourd");
DoSomething (1, "gourd", "cucumber");
Console.ReadLine ();
}
You might think that if I had a method that signed the method with the same parameters as an optional parameter method, what would C # do with it, let's look at the following code
static void dosomething (int notoparg, string arg)
{
Console.WriteLine ("Arg1 = {0}", Arg);
}
It's overloaded with a dosomething. This method has two parameters, but there are no optional parameters,
Experiments have shown that calling 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:
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
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.
11 C # 4.0 Four new features code examples and interpretation