Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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

:(

like image 713
Nahum Avatar asked Mar 16 '12 09:03

Nahum


People also ask

Can we assign multiple values to enum?

6. Attaching Multiple Values. The Enum constructor can accept multiple values. Similarly, we can add any values we want to the enum, such as the proper case symbols, “He”, “Li” and “Be”, for example.

Can enum be double?

Enums type can be an integer (float, int, byte, double etc.) but if you use beside int, it has to be cast. Enum is used to create numeric constants in . NET framework.

Can enums have names?

Definition. An Enumeration (or enum ) is a data type that includes a set of named values called elements or members. The enumerator names are usually identifiers that behave as constants in the language.

Can we create alias of a namespace?

You can also create an alias for a namespace or a type with a using alias directive.


1 Answers

If you just want an alias, you can use a using directive:

using Enum2 = Enum1;
like image 50
Thomas Levesque Avatar answered Oct 13 '22 01:10

Thomas Levesque