1. dynamicExpandoObject
Friends who are familiar with js know that js can be written like this:
view sourceprint?1 var t = new Object();
2 t.Abc = 'something';
3t.Value = 243;
Now we can also use this feature of js dynamic language in c#, provided that a variable is declared as ExpandoObject type. For example:
view sourceprint?1 static void Main(string[] args)
2 {
3 dynamic t = new ExpandoObject();
4 t.Abc = "abc";
5t.Value = 10000;
6 Console.WriteLine("t's abc = {0},t's value = {1}", t.Abc, t.Value);
7 Console.ReadLine();
8}
A new namespace System.Dynamic has been added in C# 4.0 to support this application. I am not sure what the significance of this usage is. It is also a trial of C#'s transition to a dynamic language.
2. Generic automatic conversion
Before C# 4.0, the following code could not be compiled.
view sourceprint?1 IEnumerable<object> objs = new List<string> {
2 "I'm 0","I'am 1","I'am 2"
3};
However, in C# 4.0, this kind of declaration is allowed, but it is limited to generic interfaces. Similar methods for generic types are not allowed. The following code has a compilation error.
view sourceprint?1 List<object> objList = new List<string> {
2 "I'am 0","I'am 1","I'am 2"
3};
3. The optional parameters of method parameters are as follows: the syntax of method declaration
view sourceprint?1 static void DoSomething(int notOptionalArg,string arg1 = "default Arg1", string arg2 = "default arg2") {
2 Console.WriteLine("arg1 = {0},arg2 = {1}",arg1,arg2);
3}
This method has three parameters. The first is a required parameter, the second and third are optional parameters, and they all have a default value. This form is useful for several method overloads with fixed parameters.
Called as follows:
view sourceprint?1 static void Main(string[] args)
2 {
3 DoSomething(1);
4 DoSomething(1, "Gourd");
5 DoSomething(1, "Gourd", "Cucumber");
6 Console.ReadLine();
7}
Maybe you will think, if I have a method with the same method signature as an optional parameter method without selecting a parameter, how will C# handle it? Let’s take a look at the following code
view sourceprint?1 static void DoSomething(int notOpArg, string arg)
2 {
3 Console.WriteLine("arg1 = {0}", arg);
4}
I overloaded another DoSomething method. This method has two parameters, but no optional parameters. Experiments have proved that calling
When DoSomething(1,"arg"), methods without optional parameters will be executed first.
4. Named parameters of method parameters Named parameters allow us to assign parameter values by specifying parameter names when calling the method. In this case, the order of the parameters can be ignored. The following method declaration:
view sourceprint?1 static void DoSomething(int height, int width, string openerName, string scroll) {
2 Console.WriteLine("height = {0},width = {1},openerName = {2}, scroll = {3}",height,width,openerName,scroll);
3}
We can call the method declared above like this
view sourceprint?1 static void Main(string[] args)
2 {
3 DoSomething( scroll : "no",height : 10, width : 5, openerName : "windowname");
4 Console.ReadLine();
5}
Obviously this is syntactic sugar, but it makes sense when there are many method parameters and can increase the readability of the code.
http://www.cnblogs.com/yukaizhao/archive/2010/05/24/csharp-40-dynamic-optional-argument.html