Example and interpretation of four new features in C #4.0 in,

Source: Internet
Author: User

Example and interpretation of four new features in C #4.0 in,

Abstract: Let's take a look at the four important features in C #4.0 in combination with code instances.

In previous articles, we have introduced the history of C # and new features of C #4.0, including dynamic, naming and optional parameters, dynamic import, covariant, and inverter. 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;

We can also 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.

 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 "};

In C #4.0, this declaration is allowed, 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 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:

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 with the same method signature as an optional parameter method, how does C # handle it? Let's look at the following code:

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

Here, another DoSomething method is overloaded with two parameters, but there is no optional parameter,

The experiment proves that when DoSomething (1, "arg") is called, the method with no optional parameters is executed first.

 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:

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.

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.