SoFunction
Updated on 2025-03-06

Deepening the detailed introduction of new features dynamic, optional parameters, and named parameters in C# 4.0

1.dynamic ExpandoObject
Friends who are familiar with js know that js can be written like this:
Copy the codeThe code is as follows:

var t = new Object();
= ‘something';
= 243;

Now we can also use the features of this JS dynamic language in C#, provided that a variable is declared as ExpandoObject type. As shown in the following example:
Copy the codeThe code is as follows:

static void Main(string[] args)
{
dynamic t = new ExpandoObject();
= "abc";
= 10000;
("t's abc = {0},t's value = {1}", , );
();
}

A new namespace has been added to C# 4.0 to achieve support for this application. I don’t know what the meaning of this usage is now, and it is also a test of the transition from C# to dynamic languages.
2. Generic automatic conversion
The following code before C# 4.0 cannot be compiled and passed
Copy the codeThe code is as follows:

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 of generic types are not allowed. The following code has compile errors.
Copy the codeThe code is as follows:

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

3. Optional parameters for method parameters
The syntax declared by the following method
Copy the codeThe code is as follows:

static void DoSomething(int notOptionalArg,string arg1 = "default Arg1", string arg2 = "default arg2") {
    ("arg1 = {0},arg2 = {1}",arg1,arg2);
}

This method has three parameters, the first is a required parameter, the second and the third are optional parameters, and they all have a default value. This form is useful for several method overloading of fixed parameters.
Called as follows:
Copy the codeThe code is as follows:

static void Main(string[] args)
{
    DoSomething(1);
DoSomething(1, "Gourd");
DoSomething(1, "Gourd", "cucumber");
    (); 
}

Maybe you will think of what if I have a method that does not choose a certain parameter signature method, what will C# do if it is not selected as the optional parameter? Let's look at the following code
Copy the codeThe code is as follows:

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

I overloaded another DoSomething method with two parameters, but there are no optional parameters. The experiment proves that the call
When DoSomething(1,"arg"), methods without optional parameters are preferred.
4. Naming parameters of method parameters
Named parameters allow us to specify parameter names when calling a method to assign values ​​to the parameters. In this case, the order of parameters can be ignored. The following method declaration:
Copy the codeThe code is as follows:

static void DoSomething(int height, int width, string openerName, string scroll) {
    ("height = {0},width = {1},openerName = {2}, scroll = {3}",height,width,openerName,scroll);
}

We can call the method declared above in this way
Copy the codeThe code is as follows:

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

Obviously this is a syntactic sugar, but it makes sense when there are many method parameters and can increase the readability of the code.