Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force property implementation on derived classes

Tags:

c#

I have Person.cs which will be implemented by, for example, the Player class. Person has an ExperienceLevelType property.

I want to force all classes that derived from Person to implement their own version of the ExperienceLevelType property.

public abstract Person
{
    public enum ExperienceLevel { Kid, Teenager}
    public virtual ExperienceLevel Experience {get; set;}
}

public abstract Player:Person
{
    public override ExperienceLevel Experience
    {

    }
}
like image 658
user1765862 Avatar asked Nov 26 '12 19:11

user1765862


1 Answers

That's what abstract is for:

public abstract class Person
{
    public enum ExperienceLevel { Kid, Teenager}
    public abstract ExperienceLevel Experience { get; set; }
}

If you want to force derived classes to implement the property themselves while at the same time providing some reusable scaffolding to help them, expose the scaffolding as protected members inside Person.

like image 162
Jon Avatar answered Oct 17 '22 14:10

Jon