属性和方法之间的区别
例如,当涉及到返回值时,哪一个更好,
public int EmployeeAge
{
get{return intEmployeeAge};
}
哪
public int EmployeeAge()
{
return intEmployeeAge;
}
一个更好,为什么?当我们遇到上述情况时,最佳编程实践是什么?
Which one is better to use when it come to return value for example
public int EmployeeAge
{
get{return intEmployeeAge};
}
And
public int EmployeeAge()
{
return intEmployeeAge;
}
Which one is better and why? And what is best programming practice to use when we have secnario like above ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
属性是表达对象特征的一种有用方式,允许以通用方式获取/设置,可供数据绑定、反射和序列化等 API 使用。因此,对于对象的简单值,属性很方便。属性不能接受参数,不应该有明显的副作用*,并且应该快速且可重复地返回。此外,不存在“扩展属性”(镜像扩展方法)或泛型属性之类的东西。
(*=延迟加载等并不罕见,但是)
方法(C# 没有函数)更适合表达改变状态的事物,或者期望花费一些时间和时间的事物。不一定是可重现的。它们往往不适用于绑定/序列化等。
请注意,属性实际上只是编写方法的一种特殊方式。 功能差异很小。这一切都是为了表达意图。然而,您不想公开的一件事是字段(实际的
intEmployeeAge
实例变量)。所以我会:
或者只是(如果在
Employee
对象上):当然......那么问题就变成“以什么单位?”我想那是几年?
Properties are a useful way of expressing a feature of an object, allowing get/set in a common way that can be used by APIs like data-binding, reflection and serialization. So for simple values of the object, properties are handy. Properties can't take arguments, should not have significant side-effects*, and should return quickly and repeatably. Also, there is no such thing as an "extension property" (to mirror an extension method) nor a generic property.
(*=lazy loading etc isn't uncommon, however)
Methods (C# doesn't have functions) are better for expressing things that either change the state, or which have an expectation of taking some time and not necessarily being reproducible. They don't tend to work in binding / serialization etc.
Note that properties are actually just a special way of writing methods. There is little functional difference. It is all about expressing intent. The one thing you don't want to expose, however, is fields (the actual
intEmployeeAge
instance variable).So I would have:
or just (if on the
Employee
object):Of course... then the question becomes "in what unit?" I assume that is years?
如果您需要做的只是返回一个值,请使用属性。
如果您需要在返回值之前执行某些操作,请使用函数。
If all you need to do is return a value, use a property.
If you need to do something before returning a value, use a function.
属性保存对象数据
函数定义对象行为
看一下 -> 属性使用指南
Properties holds object data
Functions defines object behavior
Take a look at -> Property Usage Guidelines
我用 C# 编写,但我更喜欢使用 Get/Set 函数,对我来说,这是更好的方式来表达我可以从对象中获取什么以及如何更改它的状态(并且此方法按Intelisense 中的字母表也很好)。然而,如果团队更喜欢其他约定,这不是问题,但当我处理自己的项目时,阅读 API 会更容易。
从 API 来看,你无法说出你在公共 API 中更改了什么或它是只读属性,除非你使用 IDE 来显示一个小图标,显示你实际上可以做什么。
人们可以从 API 中轻松找到类型的客户端如何更改数据。
I write in C# however I prefer to use Get/Set functions, for me it's just better way to express what I can get from object and how I can change it's state (and this methods are grouped by alphabet in Intelisense which is also nice). However, if team prefers other conventions it's not a problem but when I work on my own projects it's just easier to read API.
from looking to API you can't say what you change in a public API or what it a read only property, unless you use IDE that shows you a small icon showing actually what you can do.
One can easily find from API how data can be changed by type's clients.
方法在工作完成后返回值,值是正在完成的工作的结果。我不认为这就是你正在做的事情。
属性(访问器)用于返回变量,这似乎是您想要实现的目标:
根据 MSDN:
查看此处,因为它对这些的用途给出了很好的描述。
A method returns values after work is completed and a value is the result of the work being done. I don't think this is what you are doing.
A property (accessor) is meant for returning variables, which seems to be what you're trying to achieve:
As per MSDN:
Have a look here, as it gives a very good description on the uses of these.
属性是一种以简单的方式探索类的内部数据元素的方法。我们可以使用类型安全的 get 和 set 方法来实现属性。使用调用约定隐式调用属性。属性在编译和运行时工作。
方法是包含一系列语句的代码块。方法被显式调用。
方法在运行时起作用。
Property is a way explore the internal data element of a class in a simple manner. We can implement a properties with the type-safe get and set method.Property is implicitly called using calling convention.Property works on compile and runtime.
Method is a block of code that contain a series of statements.Method is explicitly called.
Methods works on runtime.
我参加这个聚会有点晚了,但我只想提到属性和无参数“get”方法之间的另一个令人惊讶的区别。正如 @MarcGravell 指出的,延迟加载是使用属性时的常见模式,但要注意 Heisenberg Watch Window 陷阱!
I'm a little late to this party, but I'll just mention another surprising difference between a property and a parameterless "get" method. As @MarcGravell notes, lazy loading is a common pattern when using properties, but beware of the Heisenberg Watch Window gotcha!
我认为这与您正在编程的文化有很大关系。在我看来,C# / .NET 文化在这种情况下更喜欢使用属性。
我的建议:尝试与您正在使用的主要库保持一致。
但是:谨慎使用属性(或具有相同目的的函数,如上面的示例),因为它们通常是糟糕设计的标志。您想要告诉您的对象做一些事情,而不是询问它们提供信息。不过,不要对此抱有虔诚的态度,只需将其视为代码异味即可。
I think this has a lot to do with the culture you are programming in. As I see it, the C# / .NET culture would prefer to use a property for this case.
My advice: Try to be consistent with the main libraries you are using.
BUT: Be wary of using properties (or functions that serve the same purpose, as in your example above), as they are often a sign of bad design. You want to tell your objects to do stuff, as opposed to asking them for information. Don't be religious about this, though, just be aware of this as a code smell.