Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Readonly properties and fields cannot be set in derived class constructors [duplicate]

I have a class structure like the following

abstract class AMyAbstractClass {
    public readonly int MyReadonlyField;
    public int MyReadonlyProperty { get; }//read-only auto-property (syntactic sugar)
}
class MyConcreteClass : AMyAbstractClass {
    MyConcreteClass() {
        this.MyReadonlyField = 1;
        this.MyReadonlyProperty = 1;
    }
}

Which throws compilation errors

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

And

Property or indexer 'AMyAbstractClass.MyReadonlyProperty cannot be assigned to -- it is read only

Respectively.

In the first case, the error message is erroneous, since it is being set in a constructor!


I can come up with alternative ways to have immutable object properties, but why is this situation disallowed? What are good practices for this type of encapsulation?

like image 736
Elaskanator Avatar asked Aug 05 '19 15:08

Elaskanator


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

You need to propagate them via base class' constructor:

abstract class AMyAbstractClass {
    public readonly int MyReadonlyField;
    public int MyReadonlyProperty { get; }//syntactic sugar
    protected AMyAbstractClass (int fieldValue, int propertyValue) {
        this.MyReadonlyField = fieldValue;
        this.MyReadonlyProperty = propertyValue;
    }
}
class MyConcreteClass : AMyAbstractClass {
    public MyConcreteClass() 
        : base(fieldValue: 1, propertyValue:1) {
    }
}

Regarding readonly fields. Reference from ECMA-334 C# Language Specification, chapter 15.5.3:

When a field - declaration includes a readonly modifier, the fields introduced by the declaration are readonly fields . Direct assignments to readonly fields ca n only occur as part of that declaration or in an instance constructor or static constructor in the same class.

And the description for read-only auto-properties from c# 6:

... properties can be set only in the body of a constructor:

like image 176
Renat Avatar answered Sep 21 '22 05:09

Renat