C #3.0 overview of new features

Source: Internet
Author: User
Tags list of attributes
Source: Van's zone-blog
Microsoft has been operating frequently recently. The C #2.0 specification has just been released. I have not yet fully digested all the new features in C #2.0. Now I have introduced C #3.0, in just a few years, Microsoft has made two major upgrades to C #, adding many new features to C #, making C # more powerful and modern. Next I will list the highlights of the new features in C #3.0 for you to quickly browse.
First feature: implicitly typed local variables

This feature is very simple. For some JavaScript shadows, we can use the "Var" keyword to declare local variables without specifying the exact type of variables, the exact type of a variable can be inferred from the initial value when the variable is declared. In this way, we can greatly simplify the workload of declaring local variables. The following is an example:

1: Class localvariables: apprunner. abstractapplication
2 :{
3: Public override void run ()
4 :{
5: var intvalue = 5;
6: var stringvalue = "this is a string ";
7: var customclass = new localvariables ();
8: var intarray = new int [3] {1, 2, 3 };
9:
10: foreach (VaR value in intarray)
11: console. writeline (value );
12 :}
13 :}
The aboveCodeWill be parsed:

1: Class localvariables: apprunner. abstractapplication
2 :{
3: Public override void run ()
4 :{
5: int intvalue = 5;
6: String stringvalue = "this is a string ";
7: localvariables customclass = new localvariables ();
8: int [] intarray = new int [3];
9:
10: foreach (INT value in intarray)
11: console. writeline (value );
12 :}
13 :}
Note that because the variable type is inferred from the initial value of the variable, you must specify the initial value for the variable while declaring the variable. In addition, the variable is not of no type. Once the variable is initialized, the type is determined, and then only some type of value can be stored, for example, the stringvalue Type above is inferred to be string, so this variable can only save values of the string type.
Second feature: anonymous type

Sometimes we need to temporarily Save the intermediate results of some operations, especially when these intermediate results are composed of multiple parts, we often declare a new type, to save these intermediate results. On the surface, it seems that this is normal, and after careful consideration, we will find that this new type only serves this function and will not be used elsewhere, defining a new type for this function is indeed troublesome.

Now, the anonymous Type feature in C #3.0 can solve the problem mentioned above. With the anonymous type, we can simply use new {attribute name 1 = value 1, attribute name 2 = value 2 ,....., the property name n = value n} is used to create a new type in the function. See the following example:

1: Class anonymoustype: apprunner. abstractapplication
2 :{
3: Public override void run ()
4 :{
5: var anonymoustype1 = new {
6: cardNumber = "10001", name = "Van's", sex = true
7 :};
8:
9: console. writeline (anonymoustype1.cardnumber );
10: console. writeline (anonymoustype1.name );
11:
12: var anonymoustype2 = new {
13: cardNumber = "10002", name = "Martin", sex = true
14 :};
15:
16: anonymoustype2 = anonymoustype1;
17 :}
18 :}
Only field members are allowed in the new type, and the types of these fields are also inferred from the type of the initial value. If the field names, sequence, and initial values of the new type are consistent when the new anonymous type is declared, the same anonymous type will be generated, therefore, in the above example, anonymoustype1 and anonymoustype2 are of the same type and can be assigned a value for anonymoustype2 = anonymoustype1.
Third feature: implicitly typed array

This feature is an extension of the implicitly typed local variables. With this feature, we can easily create arrays. We can directly use the "new []" keyword to declare the array, followed by the array's Initial Value List. Here, we do not directly specify the array type. The array type is inferred from the initialization list.

1: Class anonymoustypearray: apprunner. abstractapplication
2 :{
3: Public override void run ()
4 :{
5: var intarray = new [] {1, 2, 3, 4, 5 };
6: var doublearray = new [] {3.14, 1.414 };
7: var anonymoustypearray = new [] {
8: New {name = "Van's", sex = false, Arg = 22 },
9: New {name = "Martin", sex = true, Arg = 23}
10 :};
11:
12: console. writeline (intarray );
13: console. writeline (doublearray );
14: console. writeline (anonymoustypearray [0]. Name );
15 :}
16 :}
In the above Code, the anonymoustypearray variable Declaration uses both the implicit type array and the anonymous type. First, the anonymous type is created, and then the Initial Value List is used to deduce the exact type of the array.
Fourth feature: object constructor

When declaring an array, We can initialize it at the same time, which saves a lot of trouble. However, when creating class objects, this trick will not work, we can either call the constructor of this class to initialize the object or manually initialize the object. These two methods are not convenient. When using constructors to initialize objects, we may need to compile multiple overloaded versions of constructors for some flexibility, which is really troublesome.

The object constructor added in C #3.0 makes initialization of objects extremely simple. We can use an array-like initialization method to initialize objects of the class, the method is to follow the class member initialization Code directly after the expression for creating the class object. Example:

1: Class Point
2 :{
3: Public int X {Get; set ;}
4: Public int y {Get; set ;}
5:
6: Public override string tostring ()
7 :{
8: Return "(" + X. tostring () + "," + Y. tostring () + ")";
9 :}
10 :}
11:
12: Class rectangle
13 :{
14: Public Point P1 {Get; set ;}
15: Public point P2 {Get; set ;}
16:
17: Public rectangle ()
18 :{
19: p1 = new point ();
20: P2 = new point ();
21 :}
22:
23: Public override string tostring ()
24 :{
25: Return "P1:" + p1 + ", P2:" + P2;
26 :}
27 :}
28:
29: Class objectbuilder: apprunner. abstractapplication
30 :{
31: Public override void run ()
32 :{
33: Point thepoint = new point () {x = 1, y = 2 };
34: console. writeline ("Point (x, y) = {0}", thepoint );
35:
36: rectangle therectangle = new rectangle (){
37: p1 = {x = 1, y = 1}, P2 = {x = 100, y = 200}
38 :};
39: console. writeline (therectangle );
40 :}
41 :}
When defining the X and Y attributes of the Point class, we only need to write the get and set accessors Declaration for this attribute, C # The Compiler automatically generates the default get and set operation code for us. This feature is very useful when we need to define simple attributes.

With the new point () {x = 1, y = 2} statement, we can easily initialize the Point class. When creating class objects, we can initialize class objects as needed, as long as the class creation expression is followed by the list of attributes to be initialized, you can assign an initial value to the attribute to be initialized without writing the initial values of all attributes.

In the initialization expression of therectangle object, we first initialize the P1 attribute. However, the P1 attribute is also a custom type, so the initialization of the P1 attribute is another type (point) to initialize more complex types.

To be continued .........

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.