C#对象初始化器
程序员文章站
2022-05-11 09:05:13
...
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(); } } }
以上就是C#对象初始化器的内容,更多相关内容请关注PHP中文网(www.php.cn)!