-2

Is there a way to interface with or inherit the enum of another class? Obviously I can move the enum outside of the class, but I am curious if a reference can be made:

public class deferment
{
    public enum test
    {
        test = 0,
        live
    }
}
public class defermentLog
{
    public enum test1 : deferment:test //this is where I want to reference
    {
    }
    public test1 action()
    {
        return test1.live;
    }
}
David L
  • 32,885
  • 8
  • 62
  • 93
  • 2
    an enum regardless of where it's defined is just a type. It's not bound to an instance and inheritance is, so what are you trying to accomplish? – Rune FS Apr 30 '15 at 19:04
  • 1
    And you can't inherit one enum from another regardless of where they come from. – Jon Skeet Apr 30 '15 at 19:05
  • 1
    Have you looked at this: http://stackoverflow.com/a/757731/2777098 – display name Apr 30 '15 at 19:05
  • `enum` can't inherit from another `enum`, you can use the `:` operator to define the underlying type of the `enum` (like `char`, `int`, `short`, etc), but not to inherit or "reference" another `enum`. – Ron Beyer Apr 30 '15 at 19:05
  • Context on what you are trying to achieve would help here. – juharr Apr 30 '15 at 19:06
  • In the DefermentLog class, I want to call the Test enum from the Deferment class without "deferment.test". My curiosity is if some reference can be made, such as an interface, which will allow me to define a property local to the DefermentLog class. Just curious. – user1760769 Apr 30 '15 at 19:07
  • @user1760769 If you don't want the enum to be scoped to `deferment`, don't put it inside `deferment`. The whole *point* of nested types is to scope them to their containers. – Avner Shahar-Kashtan Apr 30 '15 at 19:09
  • @user1760769 If this is just to avoid the class name before the enum name then as you stated, just move the enum out of the class. – juharr Apr 30 '15 at 19:09
  • Enumerations in .Net are really just syntactical sugar to read only static fields in a static class. As such they can not be inherited. – Matthew Whited Apr 30 '15 at 19:09

1 Answers1

2

In that case, yes, you can.

namespace ConsoleTests
{
    using TestAlias = Class1.test;

    public class Class1
    {
        public enum test
        {
            test,
            live
        }
    }

    public class Class2
    {
        public void x()
        {
            TestAlias t = TestAlias.live;
        }
    }
}

Its called a type alias, and its defined like this: using TestAlias= Class1.test;

It should be noted though that you have to define that alias in the file that you use it and it does not copy over to other files, so you have to define it in every one you use it.

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
  • This is not inheritance. If you look at the compiled IL the original type is being referenced. – Matthew Whited Apr 30 '15 at 19:12
  • @MatthewWhited its not meant to be, based on the comments to the question he just wanted to use the type name without the long winded text. I almost never use aliases, but it can be done. I wrote the comment about enums not being inheritable in the main comments... His attempt in the post wasn't inheritance, he was trying to make a reference to the other type, just not stated well. – Ron Beyer Apr 30 '15 at 19:13
  • Feel free but stuff like this will cause confusion later. If you were going to use this technique I would suggest a global/base class and use a similar using statement is both classes. But when you use the value later will will contain the name from the original location unless you declare yet another alias at the point of use. – Matthew Whited Apr 30 '15 at 19:16
  • OK. I see what you are all saying. Again, I am not saying this is the only way I want to do this, I just wanted to know if something of the sort was possible. I just stated my questions poorly, as I am still learning. Thanks – user1760769 Apr 30 '15 at 19:17