C# - 自动属性和返回支持字段之间的区别?
我想象的简单问题,但是这些代码行之间有什么区别:
代码 1
public int Temp { get; set; }
和
代码 2
private int temp;
public int Temp { get { return temp; } }
我的理解是,代码 1 的自动属性将执行与代码 2 完全相同的功能?
我正在阅读 Head First C#,我发现很难理解为什么它使用两种不同的方式来做同一件事?
Simple question I imagine, but what is the difference between these lines of code:
Code 1
public int Temp { get; set; }
and
Code 2
private int temp;
public int Temp { get { return temp; } }
My understand was that an automatic property as per Code 1 would perform the exact same function as Code 2?
I'm reading Head First C# and I'm finding it hard to understand why it's using two different ways of doing the same thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Code1 和 Code2 之间的主要区别在于,在 #1 中,该属性是可设置的。
您可以使用自动属性实现相同的目标,因为 setter 可以是私有的:
自动属性是在 C#3 中添加的,实际上只是使用字段的较长版本的语法糖。如果您不需要直接访问该字段,则没有理由不使用自动属性。自动属性相当于使用字段 - 编译器为您生成字段,只是在代码中无法访问它。
The primary difference between your Code1 and Code2 is that in #1, the property is settable.
You can achieve the same thing using automatic properties, because the setter can be private:
Automatic properties was added in C#3, and is really just syntactic sugar for the longer version using a field. If you don't need to access the field directly, there is no reason not to use automatic properties. Automatic properties are equivalent to using a field - the compiler generates the field for you, it is just not accessible in code.
“automagic”属性只是一个“简写”符号:
只是输入简单得多,
但功能相同。只是一个很好的“速记”来提高您的工作效率,但实际上没有额外或神奇的功能。
The "automagic" property is just a "short-hand" notation:
is just a lot simpler to type than
but functionally equivalent. Just a nice "shorthand" to improve your productivity, but no additional or magic functionality, really.
第一个是可写属性。
它相当于
(除了不能直接使用 backingfield),但它需要 1 行代码而不是 5 行。
当编写具有 5 或 6 个简单属性的类时,自动属性可以使类变得更短。
您可以通过编写来创建只读自动属性
The first one is a writable property.
It's equivalent to
(except that you can't use the backingfield directly), but it requires 1 line of code instead of five.
When writing classes with 5 or 6 simple properties, auto-properties can make the classes much shorter.
You can make read-only auto-properties by writing
如果您的第二个示例同时具有 getter 和 setter,那么它们在功能上是等效的。
就目前情况而言,第一个可以公开获取,但不能公开设置。您还可以使用自动属性实现相同的目标:
如果您好奇,自动属性仍然可以获得支持的私有字段。这部分只是由编译器为您处理,这样生活就更容易了。
If your second example had both a getter and a setter, they would be functionally equivalent.
As it stands now, the first is publicly gett-able but can't be set publicly. You could also achieve the same thing using auto properties:
And in case you're curious, automatic properties still get a backing private field. That bit is just handled by the compiler for you so that life is easier.
至于我使用带有支持字段的属性的原因是当我想在获取或设置属性时做其他事情时。例如,嵌入到属性本身的验证例程或缓存等...
否则,对于简单的获取和设置,我将使用自动属性格式。它更紧凑并且涉及更少的编码,我认为这是一件好事。
As for the reason why I would use property with a backing field is when I want to do something else when getting or setting the property. For example, a validation routine embedded into the property itself, or caching, etc...
Otherwise, for simple get and set, I'd use the automatic property format. It's more compact and involves less coding which I think it's a good thing.