私人领域及私人性质的差异私人、差异、性质、领域

2023-09-02 20:47:05 作者:章鱼小肉丸

使用的,而不是私人领域

What is the difference between using Private Properties instead of Private Fields

private String MyValue { get; set; }

// instead of

private String _myValue;

public void DoSomething()
{
   MyValue = "Test";

   // Instead of

   _myValue = "Test";
}

有没有性能问题?或者只是一个命名约定?

Is there any performance issue ? or just a naming convention ?

推荐答案

私有属性,可以抽象的内部数据,以便更改内部重新presentation不需要影响你实现的其他部分,甚至在同一个班级。私人领域不提供这种优势。在C#3.0自动属性,我很少看到有必要直接实现领域 - 私人或公共

Private properties allow you to abstract your internal data so that changes to the internal representation don't need to affect other parts of your implementation, even in the same class. Private fields do not offer this advantage. With automatic properties in C# 3.0, I rarely see a need to implement fields directly -- private or public.