using System; using System.Collections.Generic; using System.Linq;using System.Text; namespace 对象初始化器 { class Program { static void Main(string[] args) { //第二种初始化 var s1 = new student("张三",23); Console.WriteLine(s1.ToString()); //第一种初始化 var s2 = new student { name = "李四", age = 34 }; Console.WriteLine(s2.ToString()); //第三种 var s3 = new student("王五",30) { ID=1}; } } public class student { public string name { set; get; } public int age { set; get; } public int ID { set;get;} //第一种构造函数 public student() { } //第二种构造函数 public student(string Name, int Age) { name = Name; age = Age; } //方法重载 public override string ToString() { return name + ":" + age.ToString(); } } }
The above is the content of C# object initializer. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!