Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should use Readonly and Get only properties

Tags:

c#

.net

In a .NET application when should I use "ReadOnly" properties and when should I use just "Get". What is the difference between these two.

private readonly double Fuel= 0;  public double FuelConsumption {     get     {         return Fuel;     } }         

or

private double Fuel= 0;  public double FuelConsumption {      get      {           return Fuel;      } } 
like image 563
Ram Avatar asked Apr 27 '10 08:04

Ram


People also ask

When should you use a read only field?

In a field declaration, readonly indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class. A readonly field can be assigned and reassigned multiple times within the field declaration and constructor.

When property contains only get method then we get?

As discussed, if a property contains the only get accessor, then we will call it a read-only property. Following is the example of creating read-only properties in the c# programming language.

What is the purpose of readonly modifier?

Readonly Fields: In C#, you are allowed to declare a field using readonly modifier. It indicates that the assignment to the fields is only the part of the declaration or in a constructor to the same class.

What is the purpose of read only variables?

3.7 Read-Only Variables Read-only variables can be used to gather information about the current template, the user who is currently logged in, or other current settings. These variables are read-only and cannot be assigned a value.


1 Answers

Creating a property with only a getter makes your property read-only for any code that is outside the class.

You can however change the value using methods provided by your class :

public class FuelConsumption {     private double fuel;     public double Fuel     {         get { return this.fuel; }     }     public void FillFuelTank(double amount)     {         this.fuel += amount;     } }  public static void Main() {     FuelConsumption f = new FuelConsumption();      double a;     a = f.Fuel; // Will work     f.Fuel = a; // Does not compile      f.FillFuelTank(10); // Value is changed from the method's code } 

Setting the private field of your class as readonly allows you to set the field value only in the constructor of the class (using an inline assignment or a defined constructor method). You will not be able to change it later.

public class ReadOnlyFields {     private readonly double a = 2.0;     private readonly double b;      public ReadOnlyFields()     {         this.b = 4.0;     } } 

readonly class fields are often used for variables that are initialized during class construction, and will never be changed later on.

In short, if you need to ensure your property value will never be changed from the outside, but you need to be able to change it from inside your class code, use a "Get-only" property.

If you need to store a value which will never change once its initial value has been set, use a readonly field.

like image 198
Thibault Falise Avatar answered Sep 28 '22 08:09

Thibault Falise