使用私有还是使用属性? C#

发布于 2024-08-01 21:13:44 字数 597 浏览 4 评论 0原文

请注意,以下代码位于一个类中,单个类

private string _fee;
private string _receipt;

public string Fee
{
    get { return _fee; }
    private set { _fee = value; }
}

public string Receipt
{
    get { return _receipt; }
    private set { _receipt = value;}
}

public MyValue(string fee, string receipt) : this()
{
    _fee = int.Parse(receipt).ToString();
    _receipt = receipt;
}

如您所见,我的属性没有执行任何操作,所以我应该使用

_fee = int.Parse(fee).ToString();
_receipt = receipt;

Fee = int.Parse(fee).ToString();
Receipt = receipt;

Note that the following code is in a class a single class

private string _fee;
private string _receipt;

public string Fee
{
    get { return _fee; }
    private set { _fee = value; }
}

public string Receipt
{
    get { return _receipt; }
    private set { _receipt = value;}
}

public MyValue(string fee, string receipt) : this()
{
    _fee = int.Parse(receipt).ToString();
    _receipt = receipt;
}

As you can see my property does nothing so should I use

_fee = int.Parse(fee).ToString();
_receipt = receipt;

or

Fee = int.Parse(fee).ToString();
Receipt = receipt;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

烦人精 2024-08-08 21:13:44

使用这些属性,如果您使用的是 C# 3,则应该使用自动实现的属性,如下所示:

public string Fee
{
    get; private set;
}

public string Receipt
{
    get; private set;
}

public MyValue(string fee, string receipt) : this()
{
    this.Fee = int.Parse(fee).ToString();
    this.Receipt = receipt;
}

Use the properties, and if you're on C# 3 you should use automatically implemented properties like this:

public string Fee
{
    get; private set;
}

public string Receipt
{
    get; private set;
}

public MyValue(string fee, string receipt) : this()
{
    this.Fee = int.Parse(fee).ToString();
    this.Receipt = receipt;
}
满天都是小星星 2024-08-08 21:13:44

我总是使用属性 - 它为您提供了更大的灵活性:

  • 您可以稍后创建更复杂的 getter 和 setter 方法,如果需要,
  • 您可以在 getter 和 setter 上指定不同的可见性,
  • 您可以定义虚拟属性并覆盖后代类中的虚拟属性(如果需要)
  • 您可以对属性使用数据绑定,但不能对字段使用数据绑定

此外,.NET 反射在属性和字段上的行为似乎略有不同,因此如果您混合使用属性和字段,则需要了解这些细微的差异 - 如果您使用只有属性,你就可以开始了:-)

在类内部,你可以使用后备存储字段或属性 - 如果你使用该属性,你的 setter 可能会产生任何副作用(更新其他字段、记录日志)您的呼叫等)将被使用 - 如果您直接访问后备存储字段,您就可以绕过这些。 这可能是好事也可能是坏事 - 取决于您的情况。 只要注意你在做什么! :-)

马克

I would always use properties - it gives you more flexibility:

  • you can later on create more elaborate getter and setter methods, if needed
  • you can specify different visibility on getter and setter
  • you can define virtual properties and override those in descendant classes, if need be
  • you can use databinding against properties, but not fields

Also, it seems that .NET reflection behaves slightly differently on properties vs. fields, so if you have a mix of properties and fields, you need to know about those subtle differences - if you use only properties, you're good to go :-)

While inside the class, you can use either the backing store field, or the property - if you use the property, any side-effects your setter might have (updating other fields, logging your call etc.) will be used - if you access the backing store field directly, you get around those. This can be a good or a bad thing - depends on your scenario. Just be aware of what you're doing! :-)

Marc

独守阴晴ぅ圆缺 2024-08-08 21:13:44

在这种情况下,只要你的代码保持一致就没关系。

但是,如果您的属性被标记为virtual,那么在构造函数中访问它们并不是一个好主意,最好直接使用这些字段。 这是因为您的属性的行为可能会被覆盖,并且您的基类可能会调用破坏代码。


编辑:为了澄清,我只是指OP示例的方法差异。 marc_s 给出了一些重要的观点来说明为什么属性在大多数情况下都是有利的。

In this case it doesn't matter, as long as you're consistent throughout your code.

If, however, your properties were marked as virtual, then it wouldn't be a good idea to access them in your constructor, and it would be better to use the fields directly. This is because the behaviour of your properties could be overridden, and your base class could potentially call into breaking code.


Edit: Just to clarify, I'm just referring to differences in the approach for the OP's example. marc_s gives some great points as to why properties can be advantageous for most situations.

新人笑 2024-08-08 21:13:44

我总是直接使用成员。

这是因为您可能在 setter 中实现的代码无法在构造函数中正确运行,因为某些其他字段未初始化。

如果财产是虚拟的,如 womp 提到的,情况可能会变得更糟。

I would always use the members directly.

This is because you may implement code in the setter that isn't able to run correctly from within the constructor because some other fields aren't initialized.

It may become even worse if the properties are virtual as womp mentioned.

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