简体   繁体   English

C# - 自动属性和返回支持字段之间的区别?

[英]C# - Difference between Automatic Property and returning a backing field?

Simple question I imagine, but what is the difference between these lines of code: 我想象的简单问题,但这些代码行之间有什么区别:

Code 1 代码1

public int Temp { get; set; } 

and

Code 2 代码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? 我的理解是,根据代码1的自动属性将执行与代码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? 我正在阅读Head First C#,我发现很难理解为什么它使用两种不同的方式做同样的事情?

The primary difference between your Code1 and Code2 is that in #1, the property is settable. Code1和Code2之间的主要区别在于#1中的属性是可设置的。

You can achieve the same thing using automatic properties, because the setter can be private: 您可以使用自动属性实现相同的功能,因为setter可以是私有的:

public int Temp { get; private set; }

Automatic properties was added in C#3, and is really just syntactic sugar for the longer version using a field. 在C#3中添加了自动属性,对于使用字段的较长版本来说,它实际上只是语法糖。 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. 自动属性等同于使用字段 - 编译器为您生成字段,但在代码中无法访问。

The first one is a writable property. 第一个是可写属性。

It's equivalent to 它相当于

private int temp;
public int Temp { 
    get { return temp; } 
    set { temp = value; }
}

(except that you can't use the backingfield directly), but it requires 1 line of code instead of five. (除了你不能直接使用支持域),但它需要1行代码而不是5行代码。
When writing classes with 5 or 6 simple properties, auto-properties can make the classes much shorter. 在编写具有5或6个简单属性的类时,自动属性可以使类更短。

You can make read-only auto-properties by writing 您可以通过编写创建只读自动属性

public int Temp { get; private set; }

The "automagic" property is just a "short-hand" notation: “自动化”属性只是一种“短手”符号:

public int Temp { get; set; } 

is just a lot simpler to type than 打字比打字简单得多

public int Temp 
{   
   get { return _temp; }
   set { _temp = value; } 
}

but functionally equivalent. 但功能相同。 Just a nice "shorthand" to improve your productivity, but no additional or magic functionality, really. 只是一个很好的“速记”来提高你的生产力,但真的没有额外的或神奇的功能。

If your second example had both a getter and a setter, they would be functionally equivalent. 如果您的第二个示例同时具有getter和setter,则它们在功能上是等效的。

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: 您还可以使用自动属性实现相同的功能:

public int Temp { get; private set; }

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. 否则,对于简单的get和set,我会使用自动属性格式。 It's more compact and involves less coding which I think it's a good thing. 它更紧凑,编码更少,我认为这是一件好事。

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

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