C# 速记属性问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/687340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:31:09  来源:igfitidea点击:

C# Shorthand Property Question

c#properties

提问by pfunk

So here is a bit of syntax that I have never seen before, can someone tell me what this means? Not sure if this is supposed to be some shorthand for an abstract property declaration or something or what.

所以这里有一些我以前从未见过的语法,有人能告诉我这是什么意思吗?不确定这是否应该是抽象属性声明或其他东西的简写。

public Class1 myVar { get; set; }

For what its worth, Class1 is an abstract class.

就其价值而言,Class1 是一个抽象类。

采纳答案by alex

In C# 3.0 and later, auto-implemented propertiesmake property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

在 C# 3.0 及更高版本中,当属性访问器中不需要额外的逻辑时,自动实现的属性使属性声明更加简洁。它们还使客户端代码能够创建对象 当您如下例所示声明属性时,编译器会创建一个私有的匿名支持字段,该字段只能通过属性的 get 和 set 访问器访问。

// Auto-Impl Properties for trivial get and set
    public double TotalPurchases { get; set; }
    public string Name { get; set; }
    public int CustomerID { get; set; }

回答by TheTXI

Automatic getters and setters.

自动 getter 和 setter。

回答by Joel Coehoorn

It's called an automatic or auto-implementedproperty, and it's not abstract. You can use that property as-is. The compiler will create a backing store for it behind the scenes.

它被称为自动或自动实现的属性,它不是抽象的。您可以按原样使用该属性。编译器将在幕后为其创建一个后备存储。

It's very much like just using a field instead of a property, but you get property semantics and you can use it for things like databinding that require properties.

这很像只使用字段而不是属性,但是您可以获得属性语义,并且可以将其用于需要属性的数据绑定之类的事情。

回答by laktak

This is the syntax to let the compiler create a (hidden) field for you.

这是让编译器为您创建(隐藏)字段的语法。

Also very useful is:

同样非常有用的是:

public Class1 myVar{ get; private set; }

回答by DefLog

It is shorthand for a property that does nothing more that storing the value in a field. So it would be equivalent to:

它是一个属性的简写,除了将值存储在一个字段中之外什么都不做。所以它相当于:

private Class1 _myVar;

public Class1 myVar
{
  get{ return _myVar; }
  set{ _myVar = value;}
}

Its just a simple property that yoy could replace with a more complex evantuation at a later time without changing the interface.

它只是一个简单的属性,以后可以用更复杂的 evantuation 替换,而无需更改界面。

回答by Daniel

These are Auto-Implemented properties. They allow you to make you code more concise. You can read more about them here:

这些是自动实现的属性。它们允许您使代码更简洁。您可以在此处阅读有关它们的更多信息:

Auto-Implement

自动实施

You can also make the accessors have different access levels:

您还可以使访问者具有不同的访问级别:

public int MyVar { get; private set; }

公共 int MyVar { 得到;私人订制;}

This allows you to expose the property external to the class and still set it internally.

这允许您在类外部公开属性并仍然在内部设置它。

回答by Andrew Hare

It may look like an abstract property or a property from an interface but it is far from it. In order to encourage developers to use properties (as they are a best-practice for many reasons) Microsoft decided to include this feature in C# 3 to allow you to declare properties with greater ease.

它可能看起来像一个抽象属性或来自接口的属性,但它远非如此。为了鼓励开发人员使用属性(因为出于多种原因,它们是最佳实践)Microsoft 决定在 C# 3 中包含此功能,以便您可以更轻松地声明属性。

Here is the standard way of creating a property:

这是创建属性的标准方法:

String foo;

public String Foo
{
    get { return this.foo }
    set { this.foo = value; }
}

Now this requires quite a bit of typing and as developers are lazy to the core sometimes we are tempted to create public fields just to save some time.

现在这需要相当多的输入,而且由于开发人员对核心很懒惰,有时我们试图创建公共字段只是为了节省一些时间。

Now with the C# 3 compiler we can do this:

现在使用 C# 3 编译器,我们可以这样做:

public String Foo { get; set; }

While this looks a bit strange, consider the work that the compiler is doing on your behalf. The previous code gets compiled to this:

虽然这看起来有点奇怪,但请考虑编译器代表您所做的工作。之前的代码被编译成这样:

[CompilerGenerated]    
private string <Foo>k__BackingField;

public string Foo
{
    [CompilerGenerated]
    get
    {
        return this.<Foo>k__BackingField;
    }
    [CompilerGenerated]
    set
    {
        this.<Foo>k__BackingField = value;
    }
}

So even though the syntax looks a bit strange you are still creating a property exactly the way you are used to.

因此,即使语法看起来有点奇怪,您仍然可以完全按照习惯的方式创建属性。