0

As C# enums are just ints with fancy names, why can't I inherit from an enum and add more values to it? like:

enum A { x, y}
enum B : A { w, z}

I'm interested in all sides of this: technical, logical, practical...

jps
  • 20,041
  • 15
  • 75
  • 79
RSxx
  • 355
  • 1
  • 4
  • 14
  • 2
    https://stackoverflow.com/questions/757684/enum-inheritance – Roman Ryzhiy Sep 18 '20 at 12:01
  • 5
    Does this answer your question? [Enum "Inheritance"](https://stackoverflow.com/questions/757684/enum-inheritance) – Dani Sep 18 '20 at 12:02
  • Where would the inheritance be useful? If you have an `A` that's been set to `B.w` (what would typically be an allowed thing with object inheritance), what happens with code that only works with known values of `A`? – Damien_The_Unbeliever Sep 18 '20 at 12:02
  • 1
    To begin with, `enum` is a value type, so it cannot be inherited anyway. – Roman Ryzhiy Sep 18 '20 at 12:04
  • Logical: `enum` is basically a number with set of constant values. Inheritance is a characteristic of objecs in OOP, but enum aren't object themselves, they are just values for properties of object. Practical: it may be useful to define same set of values in two enums, but `A.One` can have completely different value than `B.One`, so no inheritance, no virtual tables, no unneccesary complication to a blunt copy/paste solution. – Sinatr Sep 18 '20 at 12:16
  • Thank you for your answers. @Sinatr I do not want virtual tables. I'm just interesting in saying: this is the original enum and now I want to add some new values to it by creating a new enum that "inherits" the values from the first one. The old code based on the original enum should continue working even with the new enum type, if the received values are in range of the original enum. If I do copy/paste I will not be able to pass the new enum as the old enum in a type safe way. I know about the other questions but they are not what I was looking for. Please don't close so fast. – RSxx Sep 18 '20 at 13:59

0 Answers0