C# 自动实现的 getter 和 setter 与公共字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/111461/
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
Auto-implemented getters and setters vs. public fields
提问by tclem
I see a lot of example code for C# classes that does this:
我看到很多 C# 类的示例代码执行此操作:
public class Point {
public int x { get; set; }
public int y { get; set; }
}
Or, in older code, the same with an explicit private backing value and without the new auto-implemented properties:
或者,在较旧的代码中,具有显式私有支持值且没有新的自动实现属性的情况相同:
public class Point {
private int _x;
private int _y;
public int x {
get { return _x; }
set { _x = value; }
}
public int y {
get { return _y; }
set { _y = value; }
}
}
My question is why. Is there any functional difference between doing the above and just making these members public fields, like below?
我的问题是为什么。执行上述操作与仅将这些成员设为公共字段(如下所示)之间是否存在功能差异?
public class Point {
public int x;
public int y;
}
To be clear, I understand the value of getters and setters when you need to do some translation of the underlying data. But in cases where you're just passing the values through, it seems needlessly verbose.
明确地说,当您需要对基础数据进行一些转换时,我理解 getter 和 setter 的价值。但是在您只是传递值的情况下,它似乎不必要地冗长。
采纳答案by Dexter
I tend to agree (that it seems needlessly verbose), although this has been an issue our team hasn't yet resolved and so our coding standards still insist on verbose properties for all classes.
我倾向于同意(这似乎是不必要的冗长),尽管这是我们团队尚未解决的问题,因此我们的编码标准仍然坚持所有类的详细属性。
Jeff Atwooddealt with this a few years ago. The most important point he retrospectively noted is that changing from a field to a property is a breaking changein your code; anything that consumes it must be recompiled to work with the new class interface, so if anything outside of your control is consuming your class you might have problems.
杰夫阿特伍德几年前处理过这个问题。他回顾性指出的最重要的一点是,从字段更改为属性是代码中的重大更改;任何使用它的东西都必须重新编译才能使用新的类接口,所以如果您控制之外的任何东西正在使用您的类,您可能会遇到问题。
回答by Joseph Daigle
It encapsulates setting and accessing of those members. If some time from now a developer for the code needs to change logic when a member is accessed or set it can be done without changing the contract of the class.
它封装了这些成员的设置和访问。如果某个时间之后代码的开发人员需要在访问或设置成员时更改逻辑,则可以在不更改类的约定的情况下完成。
回答by EricSchaefer
You never know if you might not need some translation of the data later. You are prepared for that if you hide away your members. Users of your class wont notice if you add the translation since the interface remains the same.
您永远不知道以后是否可能不需要对数据进行一些转换。如果你隐藏你的成员,你就准备好了。如果您添加翻译,您班级的用户不会注意到,因为界面保持不变。
回答by gizmo
The biggest difrence is that, if ever you change your internal structure, you can still maintain the getters and setters as is, changing their internal logic without hurting the users of your API.
最大的区别是,如果你改变了你的内部结构,你仍然可以保持 getter 和 setter 原样,改变它们的内部逻辑,而不会伤害 API 的用户。
回答by Brad Wilson
It's also much simpler to change it to this later:
稍后将其更改为这个也简单得多:
public int x { get; private set; }
回答by Kibbee
If you have to change how you get x and y in this case, you could just add the properties later. This is what I find most confusing. If you use public member variables, you can easily change that to a property later on, and use private variables called _x and _y if you need to store the value internally.
如果在这种情况下必须更改获取 x 和 y 的方式,则可以稍后添加属性。这是我觉得最困惑的地方。如果您使用公共成员变量,您稍后可以轻松地将其更改为属性,如果您需要在内部存储值,则可以使用名为 _x 和 _y 的私有变量。
回答by Rob Pilkington
The idea is that even if the underlying data structure needs to change, the public interface to the class won't have to be changed.
这个想法是,即使底层数据结构需要改变,类的公共接口也不必改变。
C# can treat properties and variables differently at times. For example, you can't pass properties as ref or out parameters. So if you need to change the data structure for some reason and you were using public variables and now you need to use properties, your interface will have to change and now code that accesses property x may not longer compile like it did when it was variable x:
C# 有时可以以不同的方式处理属性和变量。例如,您不能将属性作为 ref 或 out 参数传递。因此,如果您出于某种原因需要更改数据结构,并且您使用的是公共变量,而现在您需要使用属性,则您的界面将不得不更改,现在访问属性 x 的代码可能不再像变量时那样编译X:
Point pt = new Point();
if(Int32.TryParse(userInput, out pt.x))
{
Console.WriteLine("x = {0}", pt.x);
Console.WriteLine("x must be a public variable! Otherwise, this won't compile.");
}
Using properties from the start avoids this, and you can feel free to tweak the underlying implementation as much as you need to without breaking client code.
从一开始就使用属性可以避免这种情况,您可以根据需要随意调整底层实现,而不会破坏客户端代码。
回答by Rob Pilkington
AFAIK the generated CIL interface is different. If you change a public member to a property you are changing it's public interface and need to rebuild every file that uses that class. This is not necessary if you only change the implementation of the getters and setters.
AFAIK 生成的 CIL 接口是不同的。如果您将公共成员更改为属性,则您正在更改它的公共接口,并且需要重建使用该类的每个文件。如果您只更改 getter 和 setter 的实现,则这不是必需的。
回答by TheZenker
Also to be considered is the effect of the change to public members when it comes to binding and serialization. Both of these often rely on public properties to retrieve and set values.
在涉及绑定和序列化时,还需要考虑对公共成员的更改的影响。这两者通常都依赖公共属性来检索和设置值。
回答by marcospereira
Maybe just making fields public you could leads you to a more Anemic Domain Model.
也许只是将字段公开,您就可以使用更贫血的领域模型。
Kind Regards
亲切的问候