public class ClassA
{
public static readonly string processName;
}
pubic class ClassB : ClassA
{
static ClassB()
{
processName = "MyProcess.exe";
}
}
I am getting an error while compiling the above C# code.
The error says -- "A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)"
But I am assigning it in a static constructor.
The need for such a static variable is that, the base class has methods that uses this variable, but the derived classes and the base class must have different values for this variable. But the value is constant across all instances of the respective class. It must be readonly because, it must not be changed by anywhere.
What is the error in the above code? (If there is any) I do not seem to be able to spot one. The error message is not helping. As I am not doing anything wrong according to it.
If there is an error, how can I implement this functionality? I know a simple workaround would be to make it an instance variable and assign them different values in the derived classes. But that is unnecessary as the value is constant across all the instances of the respective class.