C#的簡單語法和初始化器
阿新 • • 發佈:2018-12-17
程式碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var student1 = new studentName("hha", "wang"); var student2 = new studentName { firstname = "hha", lastname = "wang" }; var student3 = new studentName { ID = 20 }; Console.WriteLine(student1.ToString()); Console.WriteLine(student2.ToString()); Console.WriteLine(student3.ToString()); Console.ReadLine(); } } public class studentName { public string firstname { get; set; } public string lastname{get; set;} public int ID {get;set;} public studentName() { } public studentName(string first,string last) { firstname = first; lastname = last; } public override string ToString() { return firstname + " " + ID; } } }
執行效果如下: