Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, January 12, 2016

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

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


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?

Answer by Yacoub Massad for Automated property with getter only, can be set, why?


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.

Answer by Rahul Nikate for Automated property with getter only, can be set, why?


Auto property feature was added to the language during C# 3.0 release. It allows you to define a property without any backing field, however you still need to use constructor to initialize these auto properties to non-default value. C# 6.0 introduces a new feature called auto property initializer which allows you to initialize these properties without a constructor like Below:

Previously, a constructor is required if you want to create objects using an auto-property and initialize an auto-property to a non-default value like below:

public class MyClass  {      public int Foo { get; }        public Foo(int foo)      {          Foo = foo;      }  }  

Now in C# 6.0, the ability to use an initializer with the auto-property means no explicit constructor code is required.

public string Foo { get; } = "SomeString";    public List Genres { get; } = new List { "Comedy", "Drama" };  

You can find more information on this here

Answer by Douglas for Automated property with getter only, can be set, why?


If it were not possible to initialize the read-only property from the constructor (or an auto-property initializer), then it would be useless, since it would always return the default value for its type (0 for numerics, null for reference types). The same semantics applied to readonly fields in all C# versions.

To define a true getter-only property (that cannot be initialized from the constructor), you need to specify what it returns as part of the definition:

public int Foo { get { return 5; } }  

Or, more concisely in C# 6:

public int Foo => 5;  

Answer by tom_redox for Automated property with getter only, can be set, why?


This is a new C# 6 feature, "Getter-only auto-properties", also known as "Auto-Property Initializers for Read-Only Properties" as detailed here.

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 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.

This article 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.

Answer by supercat for Automated property with getter only, can be set, why?


A variable declared readonly can be written within a constructor, but in languages which honor the attribute, cannot be modified after the constructor returns. That qualifier was provided as a language feature because it is often necessary for fields whose values will vary based upon constructor parameters (meaning they can't be initialized before the constructor starts) but won't have to change after constructors return, but it was only usable for variables exposed as fields. The semantics of readonly-qualified fields would in many cases have been perfect for public members except that it's often better for classes to expose members--even immutable ones--as properties rather than fields.

Just as read-write auto-properties exist to allow classes to expose mutable properties as easily as ordinary fields, read-only auto-properties exist to allow classes to expose immutable properties as easily as readonly-qualified fields. Just as readonly-qualified fields can be written in a constructor, so too with get-only properties.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.