为什么我们需要创建类变量来获取和设置属性?

发布于 2024-10-07 09:40:53 字数 538 浏览 9 评论 0原文

非常简单的问题,但我发现理解我们为什么这样做非常重要。

我可以在类中创建一个属性,如下所示:

第一种方法:

public class MyClass
{
   public string MyProperty {get;set;}
}

第二种方法:

public class MyClass
{
    private string _myProperty;

    public string MyProperty
    {
        get
        {
            return _myProperty;
        }
        set
        {
            _myProperty = value;
        }
    }
}

几乎所有文章都使用后一种方法。为什么我们需要在类中创建一个临时变量来保存字符串值。为什么我们不能只使用第一种方法呢?第二种方法有什么好处吗?创建额外的变量来存储值不是很糟糕的内存和性能吗?

Very simple question but I find it very important to understand why we do it.

I can create a property in the class as follows:

1st Approach:

public class MyClass
{
   public string MyProperty {get;set;}
}

2nd Approach:

public class MyClass
{
    private string _myProperty;

    public string MyProperty
    {
        get
        {
            return _myProperty;
        }
        set
        {
            _myProperty = value;
        }
    }
}

Almost all of the articles use the later approach. Why do we need to create a temporary variable in the class to hold the string value. Why can't we just use the first approach? Does the second approach provide any benefits? Isn't it bad memory and performance wise to create extra variable to store a value?

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

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

发布评论

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

评论(4

瞄了个咪的 2024-10-14 09:40:53

自动属性直到 C# 3.0 才添加到 C# 中,所以有很多例子或使用后一种形式的文章是在 C# 3.0 出现之前编写的。除非您需要在属性 setter 和 getter 中做额外的工作,否则没有理由选择其中之一。

Automatic properties were not added to C# until C# 3.0, so many examples or articles which use the later form were written before C# 3.0 came out. Unless you need to do extra work in your property setters and getters, there is no reason to choose one over the other.

柠檬色的秋千 2024-10-14 09:40:53

自动属性并不总是可用。另外,如果您在属性中有某种特殊操作,但不希望它在类内部执行,则声明的变量也会有所帮助。

Automatic properties were not always available. Also, if you had some kind of special action in a property, but didn't want it to be executed internal to the class, the declared variable would be helpful also.

第七度阳光i 2024-10-14 09:40:53

自动设置器自动属性自动实现的属性(第一种方法)仅适用于 .NET Framework >= 3.0。因此,Framework 3.0 发布之前撰写的文章使用第二种方法。

这两种方法是等效的。在内部,它们以相同的方式工作,因此我更喜欢编写更少的代码并尽可能使用第一种方法。

Auto-setters, automatic properties or auto-implemented properties (the first approach) are only available for .NET Framework >= 3.0. So articles written prior to the Framework 3.0 release use the second approach.

The two approaches are equivalent. Internally they work in the same way, so I prefer writing less code and using the first approach whenever possible.

情深已缘浅 2024-10-14 09:40:53

当然,如果您希望能够访问支持字段,您仍然需要编写完整的语法。例如,如果您要引发更改事件:

private string _name;
public string Name
{
    get
    {
        return _name;
    }
    set
    {
        if (_name != value)
        {
            _name = value;
            RaisePropertyChanged("Name");  // assuming this method handles raising the event
        }
    }
}

Of course if you want to be able to access the backing field you'll still need to write the full syntax. For example if you're going to raise change events:

private string _name;
public string Name
{
    get
    {
        return _name;
    }
    set
    {
        if (_name != value)
        {
            _name = value;
            RaisePropertyChanged("Name");  // assuming this method handles raising the event
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文