1

I know that you can't use inheritance with enums, so in my base class I want to define that the classes that implement it must return an enum of some type for a property.

Ideally, this would be like:

public abstract BaseEnum MyEnum { get; set; }

Then all the implementers would have to return some form of enum. But obviously BaseEnum doesn't exist. I can use object, but I'd rather not.

Is there a nice way of doing this?

Edit: I'm asking this because I essentially want to keep the name of the property in the implementing classes the same - I know that I won't be able to work with the enum in the base class, but if I didn't put it in the base, then the implementing classes could decide on their own property names for their enum, which could become unclear over time, as they would drift towards their own implementations.

Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
  • While enums inherit from `System.Enum` structure, you can't really make the derived class change the return type. The overridden method will have to declare its return type `System.Enum` too. – Mehrdad Afshari Jan 14 '10 at 08:53
  • 2
    I'm interested to hear the 'why' of this question, inheritence isn't there for enums because it doesn't really make sense: they're just byte mappings, and so aren't really interchangable? (i.e. I don't think there's a situation where ListItemType is interchangeable with DayOfWeek, for example) – Ed James Jan 14 '10 at 08:55
  • I think your problem is more social than technical. You don't need to specify that in the base class. Just add a comment somewhere and document the style and the property name that should be used. – Mehrdad Afshari Jan 14 '10 at 09:31
  • I don't necessarily think that it will be misused, it's more that I like my base classes to be as well-defined as possible! – Fiona - myaccessible.website Jan 14 '10 at 09:50

3 Answers3

3

All Enums inherit from System.Enum.

Robert Giesecke
  • 4,314
  • 21
  • 22
2

The closest you can easily come in C# is to make the type generic with a struct constraint:

public abstract class BaseClass<T> where T : struct
{
    public abstract T MyEnum { get; set; }
}

then

public class DerivedClass : BaseClass<SomeEnum>

C# won't let you specify a constraint restricting you to enum types, even though the CLI does support it. I have a project called Unconstrained Melody which uses post-build tricks to enforce such a constraint - but I wouldn't suggest you take the same approach unless you're feeling somewhat daring. The struct constraint will at least limit you to value types, which may be good enough.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Oooh I like it, I knew I had overlooked something. However, just to complicate things, I then have another class that uses a list of these base classes, so: public List> MyObjects { get; set; } which then doesn't compile as the T has to be a value type. What can I put in its place to make this work? – Fiona - myaccessible.website Jan 14 '10 at 09:31
  • Does that have to be able to hold different kinds of BaseClass (i.e. for different enums), or just several instances of the same kind? You may need to have a nongeneric base class for the base class, which then wouldn't contain that property. – Jon Skeet Jan 14 '10 at 09:35
  • Different kinds of BaseClass. I've now got a nongeneric base for the base class and it's working fine, thanks. – Fiona - myaccessible.website Jan 14 '10 at 09:47
1

You should take a look here: Enum “Inheritance”:

See section 8.5.2 of the CLI spec for the full details.
Relevant information from the spec

 * All enums must derive from System.Enum
 * Because of the above, all enums are value types and hence sealed
Community
  • 1
  • 1
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162