C# 3.0中Property {get, set}方法的用途是什么

发布于 2024-09-19 23:24:48 字数 762 浏览 3 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

漫雪独思 2024-09-26 23:24:48

使用 Properties 消除了为私有成员设置值和获取其值的方法调用。就像:

private int number = 0;
public int Number { get{ return number;} set{number = value;}}

现在你所要做的就是创建一个对象,而不是调用函数/方法来访问数字,你可以这样做:

ObjectCreated.Number = 100;
Console.WriteLine(ObjectCreated.Number);

隐式地,Number 将设置 number = 100,下一行将获取数字 100。

Using Properties removes the method calls for setting values and getting their values for private members. Like:

private int number = 0;
public int Number { get{ return number;} set{number = value;}}

now all you have to do is make an object, and instead of calling functions/methods to access the number, you can do:

ObjectCreated.Number = 100;
Console.WriteLine(ObjectCreated.Number);

implicitly, Number will set number = 100, and next line, would fetch the number which is 100.

天煞孤星 2024-09-26 23:24:48

以下代码片段

private string name;
public string Name
{
get { return name; }
set { name = value; }
}

- 相当于 Java 中的 getter 和 setter:

private String name;
public String getName() { return name; }
public void setName(String newName) { name = newName; }

在 C# 3.0 及以上版本中,仅使用以下行即可获得相同的效果:

public string Name { get; set; }

这称为自动属性,其中编译器会生成支持私有字段和 getter/setter此属性。希望这有帮助。

The following code snippet-

private string name;
public string Name
{
get { return name; }
set { name = value; }
}

is equivalent to following getter and setter in Java:

private String name;
public String getName() { return name; }
public void setName(String newName) { name = newName; }

In C# 3.0 onward, you can have the same effect using only the following line:

public string Name { get; set; }

This is known as automatic property where compiler generates a backing private field and getter/setter for this property. Hope this helps.

人│生佛魔见 2024-09-26 23:24:48

出于封装的目的,对象的字段必须对其他对象隐藏,有时对象希望将一些数据传递出去,这些数据可以是字段数据经过一些更改,也可以是多个字段数据的结果,对象可以通过属性来做到这一点。

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.

夜吻♂芭芘 2024-09-26 23:24:48

为了补充其他答案,我认为问题更多的是关于何时为什么使用属性而不是方法。

属性向调用者传达一个值或多或少是立即可用的。当调用者必须执行 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.

揪着可爱 2024-09-26 23:24:48

属性的获取部分是您检索属性值的部分。

属性的设置部分是您可以为属性分配值的部分。

这是一个教程:
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

愛放△進行李 2024-09-26 23:24:48

C# 属性类似于 Java 中的 getter 和 setter 函数。
事实上 - 它们被编译为 MSIL 方法。

C# Properties are like getter and setter functions in Java.
In fact - they are being compiled to MSIL methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文