First, the auto-implemented properties
public class the Person { // time before 3 we define attribute C #, usually so as to define the following // will first define a private field, and then define the properties that access to the field // Private String _name; // public String the Name / / { // GET the _name {return;} // SET value = {the _name;} // } // after automatically after have attributes. 3 # C // for verification does not require additional attributes, attributes can be used to automatically simplify the definition of property // no additional definition of a private field, and // do not define a private field is not at this time there is no private field, but the compiler help us to generate an anonymous private field, we do not need to code the write // reduce code we write //The following is an attribute with the attribute of the automatically defined, the effect equivalent to the above-defined attributes, but more compact than before /// <Summary> /// Name /// </ Summary> public String the Name { GET ; SET ;} }
Second, the object initialization
class Program { static void the Main ( String [] args) { the Person PERSON1 = new new the Person (); person1.Name = " Learning Hard " ; person1.Age = 25 ; the Person PERSON2 = new new the Person ( " Learning Hard " ); PERSON2. Age = 25 ; // error if the class does not have no-argument constructor will be compiled //The following statement is a call because no argument constructor to initialize the fields in the class // braces object initialization program part is the Person person3 = new new the Person {the Name = " Learning Hard " , Age = 25 }; // code below and it is equivalent to the code above, but the above parentheses are omitted constructor only the Person person4 = new new the Person () {the Name = " Learning Hard " , Age = 25 }; the Person person5 = new new the Person ( " Learning Hard " ) Age = { 25 }; } } public class Person { public string Name { get; set; } public int Age { get; set; } public Person() { } public Person(string name) { this.Name = name; } }
Third, a set of initialization
namespace ConsoleTest { class Program { static void the Main ( String [] args) { // C # code is initialized prior to collection for use 3.0 List < String > = names new new List < String > (); Names.Add ( " Learning hard1 " ); Names.Add ( " Learning hard2 " ); Names.Add ( " Learning hard3 " ); // Once you have set in C # 3.0 initialization properties, the code can be simplified @At the same time also used the following types of implicit (using var keyword) var newnames = new new List < String > { " Learning hard1 " , " Learning hard2 " , " Learning hard3 " }; } } public class the Person { public String the Name { GET ; SET ;} public int Age { GET ; SET ;} public the Person () { } public Person(string name) { this.Name = name; } } }
Fourth, the anonymous type
class Program { static void the Main ( String [] args) { // definition of anonymous type // because there do not know what type of initialization, so here it is necessary to use implicit type // At this implicit type played on a power did not effect, thus implicitly typed explanation put forward to serve the anonymous type // the anonymous type of service is presented in Linq, is a step by step plan in which the Microsoft team Console.WriteLine ( " into the anonymous type demo: " ); var PERSON1 = new new {the Name = " Learning Hard " , Age = 25 }; Console.WriteLine ( " {0} age: {1} ", Person1.Name, person1.Age); // define an anonymous type array var personcollection = new new [] { new new {the Name = " Tom " , Age = 30 }, new new {the Name = " Lily " , Age = 22 is }, new new the Name = { " Jerry " , Age = 32 }, // if added following a compilation error occurs // because the compiler can not infer to be converted to what type // new new {the Name = "Learning Hard"} }; int= totalAge 0 ; the foreach ( var Person in personcollection) { // The following code is proved Age property is strongly typed int type totalAge + = person.Age; } } }