namespace ConsoleApplication1 { public class Test { public int Id { get; set; } public string Name { get; set; } public string Country { get; set; } //public Test(int id, string name) : this(Id, Name, Country: "china") //{ //} public Test(int id, string name, string country) { Id = id; Name = name; Country = country; } public Test(int id, string name) : this(id, name, "默认值") { } public override string ToString() { return this.Id + "---" + this.Name + "---" + this.Country; } } }
using System; namespace ConsoleApplication1 { static class Program { static void Main(string[] args) { Test test = new Test(1, "wang"); Console.WriteLine(test); Test test1 = new Test(1, "wang121","lallaallaalal"); Console.WriteLine(test1); Console.ReadKey(); } } }
转载请标明出处:C#构造方法赋初始值
文章来源: C#构造方法赋初始值