C # code examples and explanations of four new features

Source: Internet
Author: User

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

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 to support this application. What is the significance of this usage,

It is also a test of C #'s transition to Dynamic Language. For more information about dynamic languages, see the May programming language ranking: Past and Present of dynamic languages.

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

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

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.