简体   繁体   中英

How to give two names to the same enum? (Type Name Alias)

I got a need for two extacly idential enums with diffrent names

I can do

enum DoorStatus{Open,Closed,Locked,Broken};
enum WindowStatus{Open,Closed,Locked,Broken};

and will probably be bit easier to read.

but Id rather not to duplicate code

in C++ id do

typedef Door,Window;

what is the soultion in C#?

and why have both you ask? I got Application That Handles a Window application that handles a Door . I want the Window application devoloper send me data using 'WindowStatus' enum the 'Door' guy use 'DoorStatus' enum.

I believe that user should know or care I got other devices that can be abstracted similarly.

EDIT: used

enum HatchStatus{Open,Closed,Locked,Broken};
using DoorStatus = HatchStatus;
using WndowStatus = HatchStatus;

EDIT2:

Error A using clause must precede all other elements defined in the namespace except extern alias declarations

:(

如果您只想要一个别名,可以使用using指令:

using Enum2 = Enum1;

I suppose the question is if you have two different enums that have the exact same values - then why keep both at all?

The best solution is to pick one and refactor everything to use just one.

Update

Now that you've updated your example, I think you should consider whether an enum is actually the best solution.

public class ObjectState
{
  public int ID { get; set; }
  public string Description { get; set; }
}//should really be immutable
 //should also implement IEquatable<ObjectState> for equality

public static readonly State_Open = new ObjectState()
  { ID = 1, Description = "Open" }
public static readonly State_Closed = new ObjectState() 
  { ID = 2, Description = "Closed" }

public static class DoorStatus
{ 
   public static readonly ObjectState Open = State_Open;
   public static readonly ObjectState Closed = State_Closed; 
}
//and WindowStatus if you want.


public class Door {
  public ObjectState State { get; set; }
} //or perhaps refactor to a shared base/interface StatefulObject

Now you can simply do:

new Door() { State = DoorStatus.Open; }

If you also implement IEquatable<ObjectState> then you can compare the equality of two object's state as well; you'd then likely implement GetHashCode as well meaning you can also use a state as a key in a grouping or dictionary.

The good thing now is that both an open window and an open door now have exactly the same state that equate directly, rather than using some syntactic sugar to get around a design that required identical enums.

Final Update

Since the above is 'horribly complex' - despite being, I think, good design; the best you can do is ensure that two separate enums at least share the same underlying values for identical states:

public enum States
{
  Open
}

public enum DoorStates
{
  Open = States.Open
}

public enum WindowStates
{
  Open = States.Open
}

Ultimately if you're using an enum you cannot get around the need to have two separately defined ones - this solution simply means that Open is equal for any object that can be in an Open state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM