In-depth introduction to the new dynamic, optional parameters, and named parameters in C #4.0

Source: Internet
Author: User

1. dynamic ExpandoObject
Anyone familiar with js knows that js can be written like this: Copy codeThe Code is as follows: var t = new Object ();
T. Abc = 'something ';
TB. Value = 243;

We can also use this js Dynamic Language Feature in c #, provided that a variable is declared as the ExpandoObject type. For example:Copy codeThe Code is as follows: static void Main (string [] args)
{
Dynamic t = new ExpandoObject ();
T. Abc = "abc ";
TB. 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.
2. Automatic generic Conversion
The following code cannot be compiled before C #4.0.Copy codeThe Code is as follows: IEnumerable <object> objs = new List <string> {
"I'm 0", "I'm 1", "I'm 2"
};

However, in c #4.0, this declaration is allowed, but it is also limited to generic interfaces. A similar generic method is not allowed. The following code has compilation errors:Copy codeThe Code is as follows: List <object> objList = new List <string> {
"I'm 0", "I'm 1", "I'm 2"
};

3. Optional parameters of method parameters
Syntax declared in the following methodCopy codeCode: 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 a required parameter, and the second and third are optional parameters. They both have a default value. This form is useful for loading several methods with fixed parameters.
Call the following code:Copy codeThe Code is as follows: static void Main (string [] args)
{
DoSomething (1 );
DoSomething (1, "gourd ");
DoSomething (1, "gourd", "cucumber ");
Console. ReadLine ();
}

Maybe you will think that if I have a method that does not select a method with the same signature as an optional parameter method, what will c # do? Let's look at the following code:Copy codeCode: static void DoSomething (int notOpArg, string arg)
{
Console. WriteLine ("arg1 = {0}", arg );
}

I have reloaded another DoSomething method. This method has two parameters, but there are no optional parameters. The experiment proves that the call
DoSomething (1, "arg") takes precedence over methods without optional parameters.
4. Named parameters of method parameters
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:Copy codeCode: 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 in this way.Copy codeThe Code is as follows: 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 many cases of method parameters and can increase the readability of the Code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.