Skip to main content
1 of 2
Roman Reiner
  • 1.1k
  • 1
  • 6
  • 11

I guess the "correct" way to do this is to have a protected constructor on the base class which requires the state name as a parameter.

public abstract class State
{
    protected State(string name)
    {
        if(String.IsNullOrEmpty(name))
            throw new ArgumentException("Must not be empty", "name");

        Name = name;
    }

    public string Name { get; private set; }
}

The concrete states then provide a public constructor which implicitly calls the base class constructor with the appropriate name.

public abstract class SomeState : State
{
    public SomeState() : base("The name of this state")
    {
        // ...
    }
}

Since the base class does not expose any other constructors (neither protected nor public) each inheriting class needs to go through this single constructor and thus needs to define a name.

Roman Reiner
  • 1.1k
  • 1
  • 6
  • 11