C# 属性:私有方法还是私有获取/设置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12252805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
property: private method or private get/set?
提问by silla
If I want to set my property in a Class private, so it should be only possible to use and set this property in this class, what is the better way? This
如果我想在一个私有类中设置我的属性,那么应该只能在这个类中使用和设置这个属性,什么是更好的方法?这个
public string Name { private get; private set }
or
或者
private string Name { get; set }
hmmm and there is also
嗯,还有
private string Name { private get; private set }
采纳答案by Jeppe Stig Nielsen
Have you tried compiling your examples? Only the middle one will translate.
你试过编译你的例子吗?只有中间的会翻译。
If you want to specify extra accessibility level keyword, you can only do it on one of the accessors (getter/setter), and that level of the accessor must be more restrictive than the accessibility of the entire property.
如果要指定额外的可访问性级别关键字,则只能在访问器之一(getter/setter)上进行,并且该级别的访问器必须比整个属性的可访问性更具限制性。
Here you see the rules: Restricting Accessor Accessibility
在这里您可以看到规则:限制访问器可访问性
回答by Tigran
The better way depends on what you want:
更好的方法取决于你想要什么:
public string Name { private get; private set }The property is publicbut noonecan read or wrote to it, except class itself. That is completely useless, so use just
private string Name { get; set }.
public string Name { private get; private set }属性是public但没有人可以读取或写入它,除了类本身。那完全没用,所以只使用
private string Name { get; set }.
In general if you view the property like a couple of methods (which actually is)
一般来说,如果你像几个方法一样查看属性(实际上是)
private string get_Name() { }
private string set_Name(value) { }
The reason of having a possibility to apply that identifiers to a property get/setbecomes evident, I hope.
get/set我希望有可能将标识符应用于属性的原因变得明显。
回答by Mr. Mr.
public string Name { get; private set; }
This is what I think you are wanting to do.
这就是我认为你想要做的。
There is no point trying to make the get private when the property is public unless you only want your class to see it. In that situation you should use:
除非您只想让您的班级看到它,否则当属性是公共的时,尝试将 get 设为私有是没有意义的。在这种情况下,您应该使用:
private string Name { get; set; }
Update: On second read, you definitely want the second example.
更新:第二次阅读时,你肯定想要第二个例子。
回答by Jon Hanna
It's worth noting that originally, C# wouldn't let you set different accesses on a getter or setter, so the only possible choices were:
值得注意的是,最初,C# 不允许您在 getter 或 setter 上设置不同的访问权限,因此唯一可能的选择是:
public string Name { get; set; }
protected internal string Name { get; set; }
internal string Name { get; set; }
protected string Name { get; set; }
private string Name { get; set; }
(For that matter, you couldn't have automatic properties and always had to do the writing to and from a backing field yourself, but we'll ignore that just because we'll have shorter examples that way).
(就此而言,您不能拥有自动属性,并且始终必须自己在支持字段之间进行写入,但我们将忽略这一点,因为我们将有更短的示例)。
It is often useful to have different accesses for the two, most often a more restrictive setter than getter, and so the likes of
对两者进行不同的访问通常很有用,通常是比 getter 更具限制性的 setter,因此诸如
public string Name { get; private set; }
was introduced.
被介绍了。
Now, by extension of that, it would seem logical enough to allow:
现在,通过扩展,它似乎足够合乎逻辑地允许:
public string Name { private get; private set; }
private string Name { private get; private set; }
However, what are these two expressing?
然而,这两个表达的是什么?
The second isn't too bad, it's just needlessly repetitious. Still though, it's quite likely that some confused thinking got us there (most likely an incomplete refactoring). Good code is as much about expressing what you are doing as making a computer do something (if anything, more so), better to have it express clearly.
第二个还不错,只是不必要的重复。尽管如此,很可能是一些混乱的想法让我们到达了那里(很可能是不完整的重构)。好的代码就像让计算机做某事一样(如果有的话,更重要的是)表达你正在做的事情,最好让它表达清楚。
Hence if you end up with the likes of { private get; private set; }then it'd likely be worth looking at again and thinking about what you really want to say here. Hurrah for it being a compiler error.
因此,如果你最终得到了这样的结果,{ private get; private set; }那么可能值得再次审视并思考你在这里真正想说的话。欢呼吧,这是一个编译器错误。
Th first case is even worse. It says "this property is public, except for the setter that is private, and the getter that is private". That's not an exception, "it's this thing, except for all the time" is no real expression of anything. Doubly hurrah the compiler for not letting us do it.
第一种情况更糟。它说“这个属性是公共的,除了私有的 setter 和私有的 getter”。那也不例外,“就是这个东西,除了所有的时间”并不是什么真正的表达。加倍欢呼编译器不让我们这样做。
回答by atiyar
If you really want what you are asking, then you don't need a property, you should use a privatefield.
如果你真的想要你所问的,那么你不需要属性,你应该使用一个private字段。
回答by user1958681
For a private member, you don't need to define accessors. Just do this:
对于私有成员,您不需要定义访问器。只需这样做:
private string _name;
回答by James Sheridan
It seems like you want
好像你想要
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
this would allow you to access private string Name.
这将允许您访问private string Name.

