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:
In C# 3.0 and later, auto-implemented properties make 开发者_开发百科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.
-- Auto-Implemented Properties (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. Again, the fact that the MSDN page on auto-implemented properties even mentions object creation appears to be a bad documentation decision.
That's a bad description on the MSDN page, unfortunately.
Object initializer syntax (new Foo { X = 10, Y = 20 }
) is completely separable from automatically implemented properties.
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.
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.
Oded has the example for the value type, so lets expand upon his Cat class
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
public string Status
{
get { return DeactivateDate != null ? "InActive" : "Active"; }
private set { }
}
精彩评论