因此,在设计不可变类时,它应该使用 get 属性吗?
public sealed class Person
{
readonly string name;
readonly int age;
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
public string Name
{
get { return name; }
}
public int Age
{
get { return age; }
}
}
或者公开公共只读字段是否有效
public sealed class Person
{
public readonly string Name;
public readonly int Age;
public Person(string name, int age)
{
Name = name;
Age = age;
}
}