Difference between property initialization syntax (automatically) in C # 6

What is the difference between the following expressions for initializing properties in C # 6:

1. Auto-property initialized by the constructor

public class Context1 { public Context1() { this.Items = new List<string>(); } public List<string> Items { get; private set; } } 

2: Property initialized from the support field

 public class Context2 { private readonly List<string> items; public Context2() { this.items = new List<string>(); } public List<string> Items { get { return this.items; } } } 

3: New Auto-Property syntax in C # 6

 public class Context3 { public List<string> Items { get; } = new List<string>(); } 

4: New Auto-Property syntax in C # 6

 public class Context4 { public List<string> Items => new List<string>(); } 
+9
source share
2 answers

Listing 3 shows the C # 6 equivalent of Listing 2, where a reinforcement field is provided under the hood.

Listing 4:

 public List<string> Items => new List<string>(); 

is equivalent to:

 public List<string> Items { get { return new List<string>(); } } 

which, as you can imagine, returns a new empty list with every access to the property.

The distinction between listings 2/3 and 4 is further explored in this Q&A with an example.

Listing 1 is just an auto property with getter and private setter. This is not a readonly property in which you can set it anywhere where you can access any type of private member. The readonly property (i.e., the getter-only property) can only be initialized in the constructor or in the property declaration, as in the readonly field.

+14
source

An auto-property is a short designation for an automatically-implemented property , where the developer does not need to explicitly declare a support field, and the compiler sets it up behind him.

1. Auto-property with a private network device

 public class Context1 { public Context1() { this.Items = new List<string>(); } public List<string> Items { get; private set; } } 

Auto properties can have different capabilities for setter and getter, specifying more restrictive accessibility for access, for which accessibility is different from accessibility of properties.

Other examples:

 public string Prop1 { get; private set; } public string Prop2 { get; protected set; } public string Prop3 { get; internal set; } public string Prop4 { protected internal get; set; } 

Access to these accessories with different availability can be obtained anywhere available for this availability, and not just from the designer.

2. Read-only property with support field

public class Context2 {private readonly List of elements;

 public Context2() { this.items = new List<string>(); } public List<string> Items { get { return this.items; } } 

} Before C # 6, the only way to set the value of a read-only property was to explicitly declare a support field and set it directly.

Since the field has a readonly accessory, it can only be installed when building an object.

3. Auto-read-only property

 public class Context3 { public List<string> Items { get; } = new List<string>(); } 

Starting with C # 6 , §2 can be processed by the compiler if it has a support field similar to read-write auto-negotiation, but in this case the support field can only be set when building an object.

4. Auto-read-only property with godter getter expression

 public class Context4 { public List<string> Items => new List<string>(); } 

When properties have a value that changes each time it receives, C # 6 allows you to declare a receiver body using lambda-type syntax.

The above code is equivalent to this:

 public class Context4 { public List<string> Items { get { return new List<string>(); } } } 
+4
source

All Articles