using system;using system.collections.generic;using System.Linq;using System.Text; Namespace object initializer {class Program {static void Main (string[] args) {//second initialization Var S1 = new Student ("Zhang San", 23); Console.WriteLine (S1. ToString ()); The first initialization of var s2 = new Student {name = "John Doe", age = 34}; Console.WriteLine (S2. ToString ()); The third var s3 = new Student ("Harry", +) {id=1}; }} public class Student {public string name {set; get;} public int Age {set; get;} public int ID {set;get;} The first constructor public student () {}///second constructor public student (string Name, int age) { name = name; Age = age; }//method overload public override string ToString () {return name + ': ' + age. ToString (); } }}
The above is the content of the C # object initializer, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!