celsound
celsound

Reputation: 59

How can I define a static instance of an abstract class?

I'm working with an abstract class in C# being inherited by multiple classes. One of the things I've been needing in my code is a static property such as "Unset", which would be a static instance of the class with it's main properties defined to an unset value. A generic example is as follows:

public abstract class Person
{
        public string Name {get; set;}
        public string PhoneNumber {get; set;}
        public static readonly Person Unset = new Person() {
            Name = "Unset Name"
            PhoneNumber = "Unset Phone"
        }
}

However I can't construct the "Unset" property because Person is an abstract class. I don't want to define the property for every class that derives from "Person". Is there a way to work around this?

Upvotes: 0

Views: 747

Answers (3)

Bernhard Hiller
Bernhard Hiller

Reputation: 2397

I don't fully understand your requirements. Do you want to make sure that a freshly created instance of a derived class has those "unset" values? That could be done with a constructor in the abstract class:

protected Person()
{
    Name = "Unset Name";
    PhoneNumber = "Unset Phone";
}

Yes, an abstract class can have a constructor, even though you cannot instantiate it directly. The constructor of a base class is called before the constructor of a derived class, so that you can overwrite those "unset" values in the constructor of a derived class whenever it has appropriate parameters.

Upvotes: 0

Zac Faragher
Zac Faragher

Reputation: 1001

The documentation shows that the abstract keyword indicates that the thing being modified has a missing or incomplete implementation. As a result, it cannot be directly created, and can only be used through inheritance.

Since you don't (currently) have any abstract members in your abstract class, you could instead go with a virtual class, which allows you to provide a default implementation that can be optionally overwritten.

public virtual class Person
{
    public virtual string Name { get; set; }
    public virtual string PhoneNumber { get; set; }
    public static readonly Person Unset = new Person() {
        Name = "Unset Name",
        PhoneNumber = "Unset Phone"
    };
}

If you don't need/want to override any of the class members in an inheriting class, you don't need to make an abstract or virtual class. Any (non-sealed) class can be inherited.

Upvotes: 0

Matias Cicero
Matias Cicero

Reputation: 26281

You can't create an instance of an abstract class.

What you can do, however, is create a new child:

public class UnsetPerson : Person
{
    public UnsetPerson() : base()
    {
        this.Name = "Unset Name";
        this.PhoneNumber = "Unset Phone";
    }
}

And then set the static property on your base class:

public abstract class Person
{
     public string Name { get; set; }
     public string PhoneNumber { get; set; }
     public static readonly Person Unset = new UnsetPerson();
}

Upvotes: 2

Related Questions