传统方法
private string firstName; public string FirstName { get { return firstName; } set { firstName = value; } }
如无特别设置,简化为:
public string FirstName { get; set; }
有特别设置,也可简写:
private string firstName; public string FirstName { get => firstName; set => firstName = value.SubString(0, 10); }
属性的初始值:
public string FirstName { get; set; } = "Itpow";
只读或只写:
public string FirstName { get; private set; }
属性值变化时获得通知
请参见:
C# 中的属性 | Microsoft Docs
相关阅读
C# 文档 - 入门、教程、参考。 | Microsoft Docs