Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a value to an inherited readonly field?

So I have a base class that has many children. This base class defines some readonly properties and variables that have default values. These can be different, depending on the child.

Readonly properties/fields allow you to change the value of the variable inside the constructor and also the definition, but nowhere else. I get a 'readonly variable can only be assigned to in a constructor' error if I try to change the value of an inherited readonly variable in the child class' constructor. Why is this and how can I work around this, without Reflection?

My intention: To allow user extensibility through scripts where they can only change certain fields once.

like image 584
Steffan Donal Avatar asked May 17 '11 21:05

Steffan Donal


People also ask

Can we assign value to readonly?

A readonly field can't be assigned after the constructor exits. This rule has different implications for value types and reference types: Because value types directly contain their data, a field that is a readonly value type is immutable.

How do I change values in readonly?

You can only change the value of a readonly variable at the constructor level of the same class. An Excerpt about readonly is : A readonly field can only be set upon field initialization or in a constructor.

How do you assign a value to a readonly variable in C#?

Use the readonly keyword in C# This means that the variable or object can be assigned a value at the class scope or in a constructor only. You cannot change the value or reassign a value to a readonly variable or object in any other method except the constructor.

Why readonly is used in C#?

In C#, a readonly keyword is a modifier which is used in the following ways: 1. 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.


1 Answers

The reason is that you can only assign to readonly fields in the constructor of that class.
According to the definition of readonly in the C# Reference (emphasis mine):

When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

To work around this, you could make a protected constructor in the base that takes a parameter for the readonly property.

An example:

using System; using System.Collections.Generic; using System.Linq; using System.Text;  namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             Base b = new Child();             Console.WriteLine(b.i);             Console.Read();         }     }      class Base     {         public readonly int i;          public Base()         {             i = 42;         }          protected Base(int newI)         {             i = newI;         }     }      class Child : Base     {         public Child()             : base(43)         {}     } } 
like image 89
Adam Houldsworth Avatar answered Sep 18 '22 13:09

Adam Houldsworth