assign value of readonly variable in private method called only by constructors

16,662

Solution 1

Despite what the other posts are saying, there is actually a (somewhat unusual) way to do this and actually assign the value in a method:

public class Foo
{
    private readonly string _field;
    public Foo(string field)
    {
        Init(out _field, field);
    }
    private static void Init(out string assignTo, string value)
    {
        assignTo = value;
    }
}

Example derived from here.

Alternatively, you can also return the value from a private method and assign it in the constructor as follows:

class Foo
{
    private readonly string _field;
    public Foo()
    {
        _field = GetField();
    }
    private string GetField()
    {
        return "MyFieldInitialization";
    }
}

Solution 2

Readonly field can only be assigned by the constructor. What you can do is to initialize the field with a method:

class Foo
{
    private readonly Bar _bar = InitializeBar();
    private Bar InitializeBar()
    {
        // Add whatever logic you need to obtain a Foo instance.
        return new Bar();
    }
}

Solution 3

Yes. Have you tried constructor chaining as an alternative to using a common method?

public StuffClass(string a, char b, int c)
{
    _a = a;
    _b = b;
    _c = c;
}
public StuffClass(string a, char b)
   : this(a, b, 2) 
{}

Solution 4

The readonly members can only assigned in the class level or on its constructor. that is the benefit from using the readonly keyword.

class Foo
{
    private readonly Foo _foo = new Foo(); // Valid
    public Foo()
    {
        _foo = new Foo(); // Valid
    }
    private void SomeMethod()
    {
        _foo = new Foo(); // Not valid
    }
}

You can use readonly as alternative to the const keyword when using classes "other that the string class", because the compiler will not allow you to assign a const to a classes.

Share:
16,662
tom
Author by

tom

Updated on June 07, 2022

Comments

  • tom
    tom 4 months

    C# compiler gave me the following error

    CS0191: A readonly field cannot be assigned to (except in a constructor or a variable initializer)

    Do I have to move the code (in my private function) into the constructor? That sounds awkward.

    Note that the private method was intended only to be called by the constructor. I expect that there is some sort of attribute that I can use to mark the method corresponding.

  • tom
    tom about 11 years
    The private method was intended only to be called by the constructor. I expect that there is some sort of attribute that I can use to mark the method corresponding.
  • Ritch Melton
    Ritch Melton about 11 years
    @tom - I don't understand what you mean about the attribute, but constructor chaining is an idiomatic C# way of doing what you are describing.
  • baltermia
    baltermia over 1 year
    That's a good tip, but not what op was asking for.
  • baltermia
    baltermia over 1 year
    This is the closest solution to op's question. Though, as how I understand his request, he wants the method to only be "callable" by the constructor. That however is to my knowledge not possible yet.