为什么曾经使用领域,而不是属性? [英] Why ever use fields instead of properties?

查看:121
本文介绍了为什么曾经使用领域,而不是属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的C#,我认为性能是一件美妙的事情。如此美妙,事实上,我不能看到任何真正的优势,使用领域,来代替。即使是私人领域,好像灵活性和模块化的特性提供充其量只能保存严重的头痛,在最坏的情况都完全没有影响。

I'm fairly new to C#, and I think properties are a wonderful thing. So wonderful, in fact, that I can't see any real advantage to using fields, instead. Even for private fields, it seems like the flexibility and modularity that properties offer can at best save you serious headaches, and at worst have no effect at all.

唯一的优势我可以看到的字段是可以内嵌初始化。但大多数的时候,你要初始化它们的构造,反正。如果你不使用内联初始化,没有任何理由不使用性质的所有时间

The only advantage I can see for fields is that you can initialize them inline. But most of the time, you want to initialize them in the constructor, anyway. If you aren't using inline initialization, is there any reason not to use properties all the time?

编辑:有些人提出了需要与备份性能字段(显式或自动)。让我们澄清我的问题:是否有任何理由使用领域的除了备份特性的?即,有没有时间 SOMETYPE someField; 最好 SOMETYPE SomeProperty {搞定;组; ?}

Some people have brought up the need to back up properties with fields (either explicitly or automatically). Let clarify my question: Is there any reason to use fields except to back up properties? I.e., is there any time that SomeType someField; is preferable to SomeType SomeProperty { get; set; }?

编辑2:DanM,Skurmedel,和Seth都给了真正有用的答案。我已经接受DanM的,因为它是最完整的,但如果有人要他们的回答归纳成一个单一的答案,我很乐意接受它。

Edit 2: DanM, Skurmedel, and Seth all gave really useful answers. I've accepted DanM's, as it is the most complete, but if someone were to summarize their responses into a single answer, I'd be happy to accept it.

推荐答案

通常情况下,属性需要支持字段,除非它们是简单的getter / setter自动属性。

Typically, properties need a backing field unless they are simple getter/setter "automatic properties".

所以,如果你是只是在做...

So, if you're just doing...

public string Name { get; set; } // automatic property



...你不需要一个领域,我同意,没有原因有一个。

...you don't need a field, and I agree, no reason to have one.

不过,如果你在做...

However, if you're doing...

public string Name
{
    get { return _name; }
    set 
    {
       if (value = _name) return;
       _name = value;
       OnPropertyChange("Name");
    }
}

...您需要 _name 支持字段。

...you need that _name backing field.

有关,不需要任何特殊的get / set逻辑私有变量,这真是一个判断是否应该做的私有财产自动或只是一个领域。我通常做一个字段,然后,如果我需要它是保护公共,我将其更改为自动属性。

For private variables that don't require any special get/set logic, it's really a judgment call whether to do a private automatic property or just a field. I usually do a field, then, if I need it to be protected or public, I will change it to an automatic property.

更新

正如亚西尔指出,如果使用自动性能,还是有潜伏在幕后的一个字段,它只是不是你实际上必须打出来。所以,底线是:性能不存储数据,它们提供了对数据的访问。字段究竟保存数据。所以,你需要他们,即使你看不到它们。

As noted by Yassir, if you use automatic properties, there's still a field lurking behind the scenes, it's just not something you actually have to type out. So, the bottom line is: properties don't store data, they provide access to data. Fields are what actually hold the data. So, you need them even if you can't see them.

更新2

对于修改后的问题...

Regarding your revised question...

有没有时间SOMETYPE someField;最好 SOMETYPE SomeProperty {搞定;组; }

is there any time that SomeType someField; is preferable to SomeType SomeProperty { get; set; }?

...一件事想到的:如果你有一个私人领域, (按照惯例私有字段),你把它叫做 _name ,即信号,你和任何人阅读你的代码,你是直接与私有数据工作。如果,另一方面,你让一切财产,以​​及(根据约定属性)打电话给你的私有财产名称,现在你不能光看变量并告诉它是私人数据。因此,只使用性能除掉一些信息。我没有尝试过的所有属性时,以了解是否是至关重要的信息,但事情肯定是丢失了。

...one thing that comes to mind: If you have a private field, and (according to convention for private fields) you call it _name, that signals to you and anyone reading your code that you are working directly with private data. If, on the other hand, you make everything a property, and (according to convention for properties) call your private property Name, now you can't just look at the variable and tell that it is private data. So, using only properties strips away some information. I haven't tried working with all properties to gauge whether that is crucial information, but something is definitely lost.

另一件事,更次要的是,公共字符串名称{;组; } 需要更多的输入(和有点混乱),比私人字符串_name

Another thing, more minor, is that public string Name { get; set; } requires more typing (and is a little messier) than private string _name.

这篇关于为什么曾经使用领域,而不是属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆