Skip to main content
 首页 » 编程设计

C#:用属性代替公共(public)变量不是很麻烦吗

2024年02月13日14dyllove98

<分区>

初学者提问:下面的方案有什么缺点吗?

public string SomeProperty { get; private set; } 

如 C# public variable as writeable inside the class but readonly outside the class

因为我绝对觉得它比:

string someProperty;  
public string SomeProperty{ 
    get {  
        return someProperty;  
    }  
}   

但是为什么C#不能有类似的东西

GetOnly string someProperty;  

哪个会和上面的一样?这不是更容易读写吗?