Object and set initiator:
1. introduce automatic attributes first:
Public Class Customer { Private String M_id; // Id attribute definition, which is familiar to everyone Public String Id { Get { Return M_id ;} Set {M_id =Value ;}}} // C #3.0 greatly simplifies the writing of this attribute (it does save a lotCodeQuantity, like !) Public Class Customer { Public String Id { Get ; Set ;}}
In C #3.0, you do not need to create private variables for the automatic attributes. Instead, you can give this job to the compiler! Of course, if you need to add some logic to get the accessor set the accessor, the extension is also very convenient.
2. Object Initiator
// We still use the previous habits as an example. Public Class Customer { Public Customer (){} Public Customer ( String PID) {ID = PID ;} Public Customer ( String PID, String Pname, String Page) {ID = PID; Name =Pname; age = Page ;} Public String Id { Get ; Set ;} Public String Name { Get ; Set ;} Public String Age { Get ; Set ;}} // Customer object initialization Customer customer = New Customer (); customer. ID = " 001 " ; Customer. Name = " Test001 " ; Customer. Age = " 28 " ; // Or use the constructor. Customer customer = New Customer ( " 001 " , " Test001 " , " 28 " );
The above code can be seen everywhere in our previous code, and this write will be replaced by more concise code with the object initiator, you do not need to write so many value assignment statements and do not need to define so many constructors for initialization;
// Object initializer Public Class Customer { Public Customer (){} Public String Id { Get ; Set ;} Public String Name { Get ; Set ;} Public String Age { Get ; Set ;}} // Initialize the customer object (you can initialize several attributes) Customer customer = New Customer {id = " 001 " , Name = " Test001 " , Age = " 28 " };
Of course, in essence, it only simplifies code writing and the background compiler automatically completes the conversion. The object initializer actually uses the compiler to assign values to the externally visible fields and attributes of the object in order. during compilation, the constructor is called implicitly to assign values to fields or attributes one by one.
In addition to setting simple attribute values during class initialization, the object initialization feature also allows us to set more complex nested attribute types.
//Initialize the customer object (you can initialize several attributes)Customer customer =NewCustomer {id ="001", Name ="Test001", Age ="28", Detail=NewCustomerdetail {address ="Sh", Deliaddress ="FJ"}};
3. Set Initiator
The Set initiator calls the icollection <t>. Add (t) in sequence for the elements in the initiator ). The object type of the application set initiator must implement the system. Collections. Generic. icollections <t> interface and specify the specified T.
Example: List < Int > Num = New List < Int > { 0 , 1 , 2 , 6 , 7 , 8 , 9 }; List <Customer> MERs = New List <customer> { New Customer {id = " 001 " , Name = " Test001 " , Age = " 28 " }, New Customer {id = " 002 " , Name = " Test002 " , Age = " 29 " }, New Customer {id = " 003 " , Name = " Test003 " , Age =" 30 " }};
This is similar to the previous list. there is no essential difference between Add. The Compiler automatically calls the list construction method without parameters, instantiate the customer one by one, and add them one by one, which is no different from our original practice, however, this is automatically processed by the compiler in the background, which saves us a lot of coding work.
Summary: from these new features, we can see that Microsoft has integrated more and more work in the underlying compiler, saving developers valuable time as much as possible, and coding is more in line with human thinking patterns; the powerful tools enable developers to focus more on business implementation without having to focus more on the underlying details. Indeed, language platforms are all tools, we should pay more attention to developing qualified products!
A copy in a blog: (original)Http://www.cnblogs.com/yuyijq/archive/2008/07/17/1244433.html)
Additional:
At the beginning, I thought that the object set initializer may be useless. Don't you just need to reduce a little bit of Code. For such simple initialization work, most code generators can work on it. Later, when studying the anonymous type, I suddenly found that, without this object initializer, the anonymous type would be more complex? Or is it hard to implement?
VaR test = new {key = "test", value = "test"}; What should I do if there is no object initializer?
The anonymous type has not been formally studied yet, but I don't think any change will be isolated. Many new features of C #3.0 are also interrelated, and there are certainly many new features to be prepared for the LINQ! (New experiences will be added as the study goes deeper)
from: http://blog.csdn.net/maotin/article/details/2977138