Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automated property with getter only, can be set, why?

Tags:

c#

c#-6.0

I created an automated property:

public int Foo { get; }  

This is getter only. But when I build a constructor, I can change the value:

public MyClass(string name) {     Foo = 5; } 

Why is it possible, even though this is get-only?

like image 605
Noam B. Avatar asked Jan 12 '16 12:01

Noam B.


People also ask

What is a getter only property?

The JavaScript strict mode-only exception "setting getter-only property" occurs when there is an attempt to set a new value to a property for which only a getter is specified.

Why use auto-implemented properties?

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property.

What is an automatic property and how is it useful C#?

Automatic property in C# is a property that has backing field generated by compiler. It saves developers from writing primitive getters and setters that just return value of backing field or assign to it. We can use attributes in many cases but often we need properties because features in different .

Which one is an auto-implemented property?

Auto-implemented properties declare a private instance backing field, and interfaces may not declare instance fields. Declaring a property in an interface without defining a body declares a property with accessors that must be implemented by each type that implements that interface.


2 Answers

This is a new C# 6 feature, "Getter-only auto-properties", also known as "Auto-Property Initializers for Read-Only Properties" as discussed in this MSDN magazine article 'C# : The New and Improved C# 6.0' by Mark Michaelis and in the C# 6.0 draft Language Specification.

The read-only field's setter is only accessible in the constructor, in all other scenarios the field is still read only and behaves as before.

This is a convenience syntax to reduce the amount of code you need to type and to remove the need to explicitly declare a private module level variable to hold the value.

This feature was seen as important as, since the introduction of Auto-Implemented Properties in C#3, mutable properties (those with a getter and setter) had become quicker to write than immutable ones (those with only a getter), meaning people were being tempted to use mutable properties to avoid having to type the code for a backing field usually required for read-only properties. There is more discussion of Auto-Implemented properties in the relevant section of the Microsoft C# Programming Guide.

This blog post, '#1,207 – C# 6.0 – Auto-Property Initializers for Read-Only Properties' by Sean Sexton Has a good explanation and example as follows:

Prior to C# 6.0, if you wanted a read-only (immutable) property, you’d typically use a read-only backing field that is initialized in the constructor, as shown below.

public class Dog  {     public string Name { get; set; }      // DogCreationTime is immutable     private readonly DateTime creTime;     public DateTime DogCreationTime      {         get { return creTime; }     }      public Dog(string name)     {         Name = name;         creTime = DateTime.Now;     } } 

In C# 6.0, you can use auto-implemented properties to implement a read-only property. You do this by using an auto-property initializer. The result is much cleaner than the above example, where we had to explicitly declare a backing field.

public class Dog {     public string Name { get; set; }      // DogCreationTime is immutable     public DateTime DogCreationTime { get; } = DateTime.Now;      public Dog(string name)     {         Name = name;     } } 

More details can also be found in the dotnet Roslyn repo on GitHub:

Auto-properties can now be declared without a setter.

The backing field of a getter-only auto-property is implicitly declared as readonly (though this matters only for reflection purposes). It can be initialized through an initializer on the property as in the example above. Also, a getter-only property can be assigned to in the declaring type’s constructor body, which causes the value to be assigned directly to the underlying field:

This is about expressing types more concisely, but note that it also removes an important difference in the language between mutable and immutable types: auto-properties were a shorthand available only if you were willing to make your class mutable, and so the temptation to default to that was great. Now, with getter-only auto-properties, the playing field has been leveled between mutable and immutable.

and in the C# 6.0 draft Language Specification (NB: The language specification is final as far as Microsoft are concerned, but it is yet to be approved as a EMCA/ISO standard, hence the 'draft'):

Automatically implemented properties

An automatically implemented property (or auto-property for short), is a non-abstract non-extern property with semicolon-only accessor bodies. Auto-properties must have a get accessor and can optionally have a set accessor.

When a property is specified as an automatically implemented property, a hidden backing field is automatically available for the property, and the accessors are implemented to read from and write to that backing field. If the auto-property has no set accessor, the backing field is considered readonly (Readonly fields). Just like a readonly field, a getter-only auto-property can also be assigned to in the body of a constructor of the enclosing class. Such an assignment assigns directly to the readonly backing field of the property.

An auto-property may optionally have a property_initializer, which is applied directly to the backing field as a variable_initializer (Variable initializers).

like image 186
tomRedox Avatar answered Oct 10 '22 11:10

tomRedox


This is a new feature in C#6 that allows you to create read-only properties and initialize their values from the constructor (or inline when you declare them).

If you try to change the value of this property outside the constructor, it would give you a compile error.

It is read-only in the sense that once you initialize its value (inline or inside the constructor), you cannot change its value.

like image 33
Yacoub Massad Avatar answered Oct 10 '22 11:10

Yacoub Massad