Today, let's take a look at the four important features in C #4.0 using code examples.
1. Dynamic expandoobject
Anyone familiar with JS knows that JS can be written like this:
VaR T = new object (); T. ABC = 'something'; T. value = 243; now we can use this JS Dynamic Language Feature in C #, provided that a variable is declared as the expandoobject type. For 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 adds a namespace system. dynamic is used to support this application. What is the significance of this usage? I am not sure yet. It is also a test of C #'s transition to Dynamic Language. For more information about dynamic languages, see the May programming language ranking: Past and Present of dynamic languages.
2. Automatic generic Conversion
The following code cannot be compiled before C #4.0.
Ienumerable <Object> objs = new list <string> {"I'm 0", "I'm 1", "I'm 2 "}; this declaration is allowed in C #4.0, but it is also limited to generic interfaces. Similar generic methods are not allowed. The following code has compilation errors:
List <Object> objlist = new list <string> {"I 'am 0", "I 'am 1", "I 'am 2"}; 3. optional parameters of method parameters
Syntax declared in 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 one is a required parameter, the second and third are optional parameters, both of which have a default value. This form is useful for loading several methods with fixed parameters. Call the following code:
Static void main (string [] ARGs) {dosomething (1); dosomething (1, "gourd"); dosomething (1, "gourd", "cucumber"); console. readline () ;}you may think about how C # will handle it if I have a method with the same signature as an optional parameter method, let's take a look at the following code:
Static void dosomething (INT notoparg, string Arg) {console. writeline ("arg1 = {0}", ARG);} here, another dosomething method is reloaded. There are two parameters, but no optional parameters. The experiment proves that dosomething (1, "Arg.
4. Named parameters of method parameters
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 );} naming parameters allow us to assign values to parameters by specifying their names when calling a method. In this case, we can ignore the order of parameters. The following method is declared:
We can call the method declared above in this way.
Static void main (string [] ARGs) {dosomething (scroll: "no", height: 10, width: 5, openername: "windowname"); console. readline ();} is obviously a syntactic sugar, but it makes sense in many cases of method parameters and can increase the readability of the Code.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/liaoxiaoli/archive/2011/01/13/6135287.aspx