简体   繁体   English

C# 自动实现的属性

[英]C# Auto-Implemented Properties

I am fairly new to auto-implemented properties and for the most of it I find them pretty straight forward but in the Microsoft site it states:我对自动实现的属性相当陌生,并且在大多数情况下,我发现它们非常简单,但在 Microsoft 站点中它指出:

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.在 C# 3.0 及更高版本中,当属性访问器中不需要额外的逻辑时,自动实现的属性使属性声明更加简洁。 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.当您如下例所示声明属性时,编译器会创建一个私有的匿名支持字段,该字段只能通过属性的 get 和 set 访问器访问。

-- Auto-Implemented Properties (MSDN) --自动实现的属性 (MSDN)

Can anyone explain what the following statement actually means with regard to auto-implemented properties: " They also enable client code to create objects. "?任何人都可以解释以下关于自动实现的属性的实际含义:“它们还使客户端代码能够创建对象。 ”?

I cannot figure out what this means.我无法弄清楚这意味着什么。

Thanks.谢谢。

I believe this refers to object initializer syntax, though why this would be the case is not clear. 我相信这是指对象初始化器语法,但为什么会出现这种情况并不清楚。 Auto implemented properties and object initializers are separate things and shouldn't be linked together this way. 自动实现的属性和对象初始值设定项是不同的东西,不应该以这种方式链接在一起。

So, with a class that looks like this: 所以,有一个看起来像这样的类:

public class Cat
{
    // Auto-implemented properties.
    public int Age { get; set; }
    public string Name { get; set; }
}

You can create objects like this: 您可以创建如下对象:

Cat cat = new Cat { Age = 10, Name = "Fluffy" };

Note: 注意:

As the comments say (and the MSDN page on object initializers declares), you can use object initializer syntax with any accessible field or property. 正如评论所述(以及对象初始化程序的MSDN页面声明),您可以将对象初始化程序语法与任何可访问的字段或属性一起使用。 Again, the fact that the MSDN page on auto-implemented properties even mentions object creation appears to be a bad documentation decision. 同样,自动实现的属性上的MSDN页面甚至提到对象创建这一事实似乎是一个糟糕的文档决策。

That's a bad description on the MSDN page, unfortunately. 不幸的是,这在MSDN页面上的描述很糟糕。

Object initializer syntax ( new Foo { X = 10, Y = 20 } ) is completely separable from automatically implemented properties. 对象初始化程序语法( new Foo { X = 10, Y = 20 } )与自动实现的属性完全分离

Object initializers can be used with any settable properties or fields (and there's even syntax for mutating "subproperties" when the "main property" is read-only); 对象初始值设定项可以与任何可设置的属性或字段一起使用(当“主要属性”是只读时,甚至还有用于改变“子属性”的语法); you don't have to use an automatically implemented property for this. 您不必为此使用自动实现的属性。

While it's nice that all these features work together, I believe it's useful to at least learn about them separately. 虽然所有这些功能一起工作很好,但我相信至少分别了解它们是有用的。 For example, automatically implemented properties could have been introduced in C# 2 without object initializers - or vice versa. 例如,自动实现的属性可以在没有对象初始化器的情况下在C#2中引入 - 反之亦然。

I think what they mean by 我认为他们的意思是什么

"They also enable client code to create objects." “它们还使客户端代码能够创建对象。”

is that the client code can init a new ref type object or assign a value type object to the auto property without having to create a private field to hold onto the data. 是客户端代码可以初始化一个新的ref类型对象或将值类型对象分配给auto属性,而不必创建一个私有字段来保存数据。

Oded has the example for the value type, so lets expand upon his Cat class Oded有值类型的示例,所以让我们扩展他的Cat类

private class Cat
{
    // Auto-implemented properties.
    public int Age { get; set; }
    public string Name { get; set; }

    public List<Cat> Kittens { get; set; }
}

Cat cat = new Cat { Age = 10, Name = "Fluffy" }; //borrowed fluffy for this example
cat.Kittens = new List<Cat>();

cat.Kittens.Add( new Cat() { Age = 0, Name = "Pinky" } );
cat.Kittens.Add( new Cat() { Age = 0, Name = "Blinky" } );

You can Add logic to getter function by accessing values of another property there by auto implementing property values您可以通过自动实现属性值访问另一个属性的值来向 getter 函数添加逻辑

 public string Status
    {
        get { return DeactivateDate != null ? "InActive" : "Active"; }
        private set { }
    }

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

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