C #3.5 Summary of new features:
I. object initialization
If a class has public fields, you can use the following code to initialize these fields when creating an object instance of the class;
Public class myclass
{
Public String field1;
Public int field2;
Public bool field3;
}
Myclass my = new myclass ();
My. field1 = "ABCD ";
My. field2 = 44;
My. field3 = true;
In C #3.0, a simpler method is provided to initialize these public variables. The Code is as follows:
Myclass my = new myclass
{
Field1 = "ABCD ",
Field2 = 44;
Field3 = true;
};
Ii. Set Initialization
In C #3.0, the initialization method for the Collection class is also improved (the initialization method is similar to an array ). Unfortunately, this initialization method only supports the following generic collection class code:
List <person> People = new list <person> {
New person {firstname = "Scott", lastname = "Guthrie", age = 32 },
New person {firstname = "bill", lastname = "Gates", age = 50 },
New person {firstname = "Susanne", lastname = "Guthrie", age = 32}
};
Iii. use VaR to define variables
In C #3.0, a new way of declaring variables is provided. This is var. With this keyword, you do not need to specify the type when declaring a variable. The variable type is determined by the compiler during initialization. The Code is as follows:
VaR Ss = "ABCD ";
MessageBox. Show (ss. GetType (). tostring ());
The code above will show system. string, which proves that the C # compiler has compiled SS into a string variable. After the SS is output, enter ". the corresponding methods and attributes of the string type variables are also listed. Therefore, we can conclude that C # regards ss as the string type rather than the object. Therefore, using VAR to define variables can also have the advantages of objects and strong types.
Using VaR to define variables has the following four features:
1. It must be initialized during definition. That is, it must be in the form of VaR S = "ABCD" instead of the following:
VaR S;
S = "ABCD ";
2. Once Initialization is complete, the variable cannot be assigned a value of a different type than the initialization value.
3. var must be a local variable.
4. Using VaR to define variables is different from object. It is the same in efficiency as defining variables using a strong type. However, I suggest you declare variables in a strongly typed manner if you know the type of the variables in advance. Otherwise, a large amount of VaR will be used, making it difficult for developers to determine the type of a variable. This is not conducive to program maintenance and upgrade.
Although var has advantages and disadvantages, I personally think that if you convert a dynamic language into a C # language, you can consider using VAR to define variables. This is because the dynamic language does not have a type. to convert it to a strong C # language, you must specify a type for the variable, but it is very difficult to determine the type beforehand, it is better to specify it as VAR, and then the C # compiler determines the specific type of the variable. What should I do if I find that the dynamic language variable has changed the type during the conversion process? This problem can be solved using the "Anonymous class" mentioned in the third part.
Iv. Anonymous
In C #3.0, a new method for creating a class is provided. The Code is as follows:
VaR my = new
{
Field1 = "ABCD ",
Field2 = 12
};
MessageBox. Show (My. field1 );
C # the compiler will automatically infer that my is an object instance of a class with two public fields. That is to say, it is equivalent to the following code:
Public class myclass
{
Public String field1;
Public int field2;
}
VaR my = new myclass ();
My. field1 = "ABCD ";
My. field2 = 25;
MessageBox. Show (My. field1 );
5. Expansion Methods
There are always many wonderful things in this world. However, the extension method described in this section is one of them. Literally, it may be difficult for readers to guess what the "Extension Method" means. However, after reading the example below, you will feel a wonderful experience.
Namespace extmethod
{
Public class class1
{
Public String S = "bill ";
}
Public class class2: class1
{
}
Public static class anyclassname
{
Public static string getname (this class1 class1)
{
Return class1.s + class1.s;
}
}
Public partial class form1: Form
{
Private void button#click (Object sender, eventargs E)
{
Class1 c = new class1 ();
MessageBox. Show (C. getname ());
Class2 c = new class2 ();
MessageBox. Show (C. getname ());
}
}
}
When I see the code above, many people may be surprised that the getname method is not available in class1 and class2. How can I obtain a getname method during the call? In fact, this is the usage of the extension method. In essence, the extension method is to insert the static method (which must be declared as static) into a class and its subclass (that is, static methods defined externally can be used in these classes ). So which class should I insert? This must be specified when defining static methods. Let's take a look at the first parameter of the getname method and use the this keyword. This indicates that this method is an extension method, and the following type is the class for inserting this method, in this example, It is class1, that is, the getname method can be used in class1 and its subclass. The above call code is equivalent to the following code:
Class2 c = new class2 ();
MessageBox. Show (anyclassname. getname (c ));
However, using C. getname may be better, and it also reduces the dependency on the class (anyclassname) where the static method is located.
Pay attention to the following points when using the extension method:
1. The class name of the extension method can be any legal class name.
2. The class of the extension method must be in the same namespace as the code using the extension method. Otherwise, the Code cannot be compiled.
3. In this example, class1 and class2 can only be declared as public, because anyclassname is declared as public. If anyclassname has no modifier, class1 and class2 can also be declared as public. That is to say, class1 and class2 must have stronger access than anyclassname. As shown in the following code:
Class class1
{
Public String S = "bill ";
}
Class class2: class1
{
}
Static class anyclassname // at this time, if the previous public is added, compilation will fail.
{
Public static string getname (this class1 class1)
{
Return class1.s + class1.s;
}
}
4. If the getname method already exists in class1 or class2, The getname priority in class1 or class2 is higher. That is to say, the extension method cannot overwrite the method with the same name (the same parameter name and type) in the original class.
Extension methods are especially useful when many classes need the same method, and these classes cannot inherit other classes. Of course, when you want to extend a class, but we don't have source code, the extension method can also be used.
Vi. Automatic attributes
Automatic attributes allow you to avoid manually declaring a private member variable and writing the get/set logic. Instead, the compiler automatically generates a private variable and default get/set operation for you.
For example, using automatic attributes, I can rewrite the above Code:
Public class person {
Public String firstname {Get; set ;}
Public String lastname {Get; set ;}
Public int age {Get; set ;}
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/liaoxiaoli/archive/2011/01/13/6135340.aspx