C# 3.0中Property {get, set}方法的用途是什么
可能的重复:
C# 自动属性
您好,
我最近从 Java 转向.net (c#) 平台。在这里,我还没有遇到太多问题......无论如何,我搞砸了
property{get, set}method. Actually I could not get up the exact meaning of these (property) according to me the same job of initializing variables or field or state of object can be done using methods. where we can declare fields as private and can access them using public method of same class.
好吧,一件简单的事情我不是程序员或雇员,但计算机科学的一般学生的目标是成为一名程序员作为全职职业。
- 非常感谢大家对我的帮助。
property{get, set}Possible Duplicate:
C# Automatic Properties
Hello,
I have recently moved towards .net (c#) platform from Java. Here I don't face much problem yet... anyway I 'm messed up with
property{get, set}
method. Actually I could not get up the exact meaning of these (property) according to me the same job of initializing variables or field or state of object can be done using methods. where we can declare fields as private and can access them using public method of same class.
Well one simple thing I am not a programmer or employee but a general students of Computer science aimed to become a programmer as full time career.
- so many thanks all of you in-advance for assisting me.
property{get, set}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 Properties 消除了为私有成员设置值和获取其值的方法调用。就像:
现在你所要做的就是创建一个对象,而不是调用函数/方法来访问数字,你可以这样做:
隐式地,Number 将设置 number = 100,下一行将获取数字 100。
Using Properties removes the method calls for setting values and getting their values for private members. Like:
now all you have to do is make an object, and instead of calling functions/methods to access the number, you can do:
implicitly, Number will set number = 100, and next line, would fetch the number which is 100.
以下代码片段
- 相当于 Java 中的 getter 和 setter:
在 C# 3.0 及以上版本中,仅使用以下行即可获得相同的效果:
这称为自动属性,其中编译器会生成支持私有字段和 getter/setter此属性。希望这有帮助。
The following code snippet-
is equivalent to following getter and setter in Java:
In C# 3.0 onward, you can have the same effect using only the following line:
This is known as automatic property where compiler generates a backing private field and getter/setter for this property. Hope this helps.
出于封装的目的,对象的字段必须对其他对象隐藏,有时对象希望将一些数据传递出去,这些数据可以是字段数据经过一些更改,也可以是多个字段数据的结果,对象可以通过属性来做到这一点。
For encapsulation purposes, object's fields must be hidden from the others, sometimes object want to pass some data to out, these data can be fields data by some changes, or can be resulted from multiple fields data, and object can do this by Property.
为了补充其他答案,我认为问题更多的是关于何时和为什么使用属性而不是方法。
属性向调用者传达一个值或多或少是立即可用的。当调用者必须执行
myObject.CustomerList
时,他期望列表已被加载并由其类缓存在字段中。他希望在对列表进行进一步操作时不需要将列表存储在局部变量中。当他必须执行
myObject.GetCustomerList()
时,他预计需要一段时间,因为要从某处检索列表,并且每次执行时他可能都会获得该列表的另一个实例调用,因此他应该将列表存储在局部变量中。To add to the other answers, I think the question is more about when and why to use properties instead of methods.
A property communicates to the caller that a value is more or less instantly available. When the caller has to do
myObject.CustomerList
, he expects that the list has already been loaded and is cached by its class in a field. He expects that he doesn't need to store the list in a local variable when doing further operations on it.When he has to do
myObject.GetCustomerList()
, he expects that it will take a while as the list would be retrieved from somewhere, and that he will maybe get another instance of the list every time he does the call, and that he thus should store the list in a local variable.属性的获取部分是您检索属性值的部分。
属性的设置部分是您可以为属性分配值的部分。
这是一个教程:
http://www.csharp-station.com/Tutorials/Lesson10.aspx
The get portion of a property is the portion where you retrieve the value of a property.
The set portion of a property is the portion where you can assign a value to the property.
Here is a tutorial:
http://www.csharp-station.com/Tutorials/Lesson10.aspx
C# 属性类似于 Java 中的 getter 和 setter 函数。
事实上 - 它们被编译为 MSIL 方法。
C# Properties are like getter and setter functions in Java.
In fact - they are being compiled to
MSIL
methods.