In depth introduction of C 4.0 features dynamic optional parameters named parameters

  • 2020-05-10 18:46:13
  • OfStack

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 this js dynamic language feature can also be used in c#, as long as one variable is declared of type ExpandoObject. The following cases:

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 new namespace, System.Dynamic, to support this application. I'm not quite sure what this means, but it's a test of c#'s transition to a dynamic language.
2. Automatic conversion of generics
The following code will not compile until C#4.0

IEnumerable<object> objs = new List<string> { 
        "I'm 0","I'am 1","I'am 2"
    }; 

However, this declaration is allowed in c#4.0, but it is limited to generic interfaces. Similar practices for generic types are not allowed. The following code has a compilation error

List<object> objList = new List<string> {  
        "I'am 0","I'am 1","I'am 2"
    }; 

3. Optional parameters for method parameters
The syntax for the following method declaration

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 arguments: the first is required, the second and third are optional, and each has a default value. This form is useful for several method overloads with fixed parameters.
Call as follows:

static void Main(string[] args) 
{ 
    DoSomething(1); 
    DoSomething(1, " gourd "); 
    DoSomething(1, " gourd ", " cucumber "); 
    Console.ReadLine();  
} 

Now, you might think, well, what happens if I have a method that is signed with a method that doesn't take the same parameter as the optional parameter method? Let's look at the code below

static void DoSomething(int notOpArg, string arg) 
{ 
    Console.WriteLine("arg1 = {0}", arg); 
} 

I overridden another DoSomething method that takes two arguments, but has no optional arguments
DoSomething(1, "arg") preferentially executes methods with no optional parameters.
4. Named parameter for the method parameter
Naming parameters allows you to assign parameters by specifying their names when calling a method, in which case you can ignore the order of the parameters. The following method declaration:

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 call the method declared above by doing this

static void Main(string[] args) 
{ 
    DoSomething( scroll : "no",height : 10, width : 5, openerName : "windowname"); 
    Console.ReadLine();  
} 

This is obviously a syntax sugar, but it makes sense in cases where the method has a lot of arguments and can increase the readability of the code.

Related articles: