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

发布于 2024-10-10 03:47:56 字数 307 浏览 0 评论 0原文

我想象的简单问题,但是这些代码行之间有什么区别:

代码 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 技术交流群。

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

发布评论

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

评论(5

薄荷→糖丶微凉 2024-10-17 03:47:56

Code1 和 Code2 之间的主要区别在于,在 #1 中,该属性是可设置的。

您可以使用自动属性实现相同的目标,因为 setter 可以是私有的:

public int Temp { get; private set; }

自动属性是在 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:

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. 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.

情深缘浅 2024-10-17 03:47:56

“automagic”属性只是一个“简写”符号:

public int Temp { get; set; } 

只是输入简单得多,

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

但功能相同。只是一个很好的“速记”来提高您的工作效率,但实际上没有额外或神奇的功能。

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.

鹿童谣 2024-10-17 03:47:56

第一个是可写属性。

它相当于

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

(除了不能直接使用 backingfield),但它需要 1 行代码而不是 5 行。
当编写具有 5 或 6 个简单属性的类时,自动属性可以使类变得更短。

您可以通过编写来创建只读自动属性

public int Temp { get; }

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.
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

public int Temp { get; }
殤城〤 2024-10-17 03:47:56

如果您的第二个示例同时具有 getter 和 setter,那么它们在功能上是等效的。

就目前情况而言,第一个可以公开获取,但不能公开设置。您还可以使用自动属性实现相同的目标:

public int Temp { get; private set; }

如果您好奇,自动属性仍然可以获得支持的私有字段。这部分只是由编译器为您处理,这样生活就更容易了。

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:

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.

一个人的旅程 2024-10-17 03:47:56

至于我使用带有支持字段的属性的原因是当我想在获取或设置属性时做其他事情时。例如,嵌入到属性本身的验证例程或缓存等...

否则,对于简单的获取和设置,我将使用自动属性格式。它更紧凑并且涉及更少的编码,我认为这是一件好事。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文