简体   繁体   English

自动生成instvar有什么意义?

[英]What is the point of auto generate instvar?

Since C# 3.0 there is the new syntax to autogenerate the instance variable from a propertie: 从C#3.0开始,有一种新语法可以根据属性自动生成实例变量:

public string Foo { get; set; } 

But there is no way to access the underlying backing field. 但是无法访问基础支持字段。 So I don't really see the point of it because declaring the same instance variable would produce the same effect without the overhead of calling the "getter" and "setter". 因此,我并没有真正理解它的意义,因为声明相同的实例变量将产生相同的效果,而没有调用“ getter”和“ setter”的开销。

public string Foo;

Declaring the property like: 像下面这样声明属性:

public string Foo { get; } 

or 要么

public string Foo { set; } 

is totally useless since we are not able to write resp. 完全没有用,因为我们无法编写resp。 read the content of the field. 阅读该字段的内容。

Does someone have a good explanation for why in C# they have put all this effort to introduce this syntactic sugar? 有人对为什么在C#中投入全部精力来引入这种语法糖有很好的解释吗?

EDIT: People seems to think that I am confusing field and properties so let me clarify what I have said. 编辑:人们似乎认为我在混淆字段和属性,所以让我澄清一下我所说的话。

If you are using a property with auto generate field: 如果您将属性与“自动生成”字段一起使用:

public string Foo { get; set; } 

Then there is no point to it, since whenever you access the properties there is the overhead call to the get_Foo() "getter" every time you are accessing the properties and since your aren't doing anything particular in the the "getter" there is not mush interest of creating this property. 那就没有意义了,因为每当您访问属性时,每次访问属性时都会对get_Foo() “ getter”进行开销调用,并且由于您在“ getter”中没有做任何特别的事情对创建此属性没有兴趣。 Creating the field would have been exactly the same and it is mush faster (optimization wise). 创建该字段将是完全相同的,并且必须更快(优化明智)。

Thanks 谢谢

public string Foo { get; set; } public string Foo { get; set; } defines a property. public string Foo { get; set; }定义一个属性。
public string Foo; defines a field. 定义一个字段。

They are different things. 他们是不同的东西。 And the former is very convenient when you actually want a property and not a field. 当您实际上需要属性而不是字段时,前者非常方便。

As for accessing, you can define different access levels: 至于访问,您可以定义不同的访问级别:

public string Foo { get; private set; }

See also Difference between Property and Field in C# 3.0+ 另请参见C#3.0+中的属性与字段之间的区别


As for your edit, what you are saying only makes sense if: 至于您的编辑,您的发言只有在以下情况下才有意义:

  1. You are 100% sure that your property accessors will never contain any actual logic. 您100%确保属性访问器永远不会包含任何实际逻辑。
  2. You are 100% sure that your field will never be data bound. 您100%确保您的字段永远不会受到数据绑定。

If any of the above is not true, then you need to define a property, even if that property seems like an empty stub. 如果以上任何一项都不成立,那么即使该属性看起来像一个空的存根,也需要定义一个属性。
Then, when later you decide to add logic to the accessors, you will not break all those clients that rely on your field being a field. 然后,当您以后决定向访问器添加逻辑时,您不会破坏所有依赖于您的字段为字段的客户端。
And if your class may be data bound, you need a property right away, as the data binding mechanisms ignore fields. 而且,如果您的类可能是数据绑定的,则您马上需要一个属性,因为数据绑定机制会忽略字段。

public string Foo; 

Is a field. 是一个领域。

public string Foo { get; set; }

Is a propery. 是一种财产。 They aren't the same thing, which has been discussed before. 他们是不一样的东西,这已经 讨论了。 One of the mainly propagated advantages is the access control: get; private set; 主要传播的优势之一是访问控制: get; private set; get; private set; is used quite often as an example. 经常用作示例。

But there is no way to access the underlying backing field. 但是无法访问基础支持字段。

Yes there is, by calling the getter. 是的,通过调用吸气剂。 Use [this.]Propertyname when you want to access it locally. 当您想在本地访问时,请使用[this.]Propertyname


public string Foo { get; } 

and

public string Foo { set; } 

Will give you compiler errors, since you have to implement both get and set . 由于必须同时实现getset ,因此会给您编译器错误。

The point of it is to make the code more concise and give you less to type. 这样做的目的是使代码更简洁,键入更少。

In your example: 在您的示例中:

public string Foo;

Is a field and isn't the same as a property: 是一个字段,与属性不同:

public string Foo { get; set; }

So the auto-property syntax has a use and is somewhat equivalent to: 因此,自动属性语法有用途,并且在某种程度上等同于:

private string _foo;
public string Foo { get { return _foo; } set { _foo = value; } }

However it doesn't create nice names, it uses safe names that would be invalid in C# (but are valid in IL). 但是,它不会创建漂亮的名称,它使用在C#中无效(但在IL中有效)的安全名称。

Property overhead, though technically possible, is usually ironed out by the JIT into direct field access. 财产开销,尽管在技术上是可能的,但通常由JIT消除,直接进入现场。

Properties are also usually the target for most data-binding frameworks. 属性通常也是大多数数据绑定框架的目标。 In reflection, a property differs from a field. 在反思中,属性不同于字段。 WPF / WinForms data binding uses properties (not public fields). WPF / WinForms数据绑定使用属性(而不是公共字段)。 Most properties use a backing field to store the data. 大多数属性都使用一个后备字段来存储数据。 The trade-off is that when in the class, you use the property still as opposed to using the backing field. 权衡取舍的是,在类中时,您仍然使用属性而不是使用背景字段。 In practice, on release-optimized code, the performance is exactly the same as the JIT resolves them into exactly the same thing. 实际上,在经过发行优化的代码上,性能与JIT将它们解析为完全一样的性能完全相同

By itself it's pointless, but you can do things like this: 就其本身而言,这毫无意义,但是您可以执行以下操作:

public string Foo { private set; get; } 

Which has a lot more value. 具有更大的价值。

Also properties and fields are not the same things. 属性和字段也不一样。

It makes defining properties faster and the code cleaner. 它使定义属性的速度更快,代码更简洁。 If you don't need a property then don't define one - but I would recommend against public fields. 如果不需要属性,则不要定义属性-但我建议不要使用public字段。 You can make the property private and accessing it doesn't do anything more than returning the IL defined field. 您可以将属性private并且对其进行访问除了返回IL定义的字段外,没有什么其他用途。 Further, if you need to do some work in the get or set then you'll have to define a backing value somehow, whether that be a field or some inherited member. 此外,如果您需要在getset做一些工作,那么您将必须以某种方式定义一个支持值,无论该值是字段还是某些继承的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM