简体   繁体   中英

How to evaluate two enum members with same value as not equal in C#?

I have this enum instance

public enum MyEnum {A1 = 0, A2 = 0}

where I want the members A1 A2 to yield the same value when evaluated but should not be considered equal when compared.

Hence in the following function

private int evalEnum(MyEnum enumInstance) {
    if(enumInstance == MyEnum.A1) 
        //some logic
    else if(enumInstance == MyEnum.A2)
        //some logic

    return (int) enumInstance;
} 

I don't want the function to enter the first if statement if enumInstance is of type A2 . I have tried to use Equals and CompareTo as well but as to my understanding they also evaluates to true ( 0 ) in the first if statement as long as A1 and A2 are assigned to the same value.

So in conclusion; it possible to evaluate A1 and A2 as not equal even if they are assigned the same value? If so, what is the best way to achieve this?

There is no way to do this with just an enum. Enums are simply int values with assigned names, there is no way to distinguish A1 from A2 since they have the same actual value.

You need some intermediate mapping, for example:

public enum MyEnum {A1 , A2 }
public Dictionary<MyEnum, int> MyEnumToValue = new (){{MyEnum.A1, 0},{MyEnum.A2, 0}};

That gives you an easy way to get the same value from A1/A2, while still allowing them to compare as not equal.

Since enum is in fact an integer type ( byte , int , long etc.) you will-nilly have to assign different values to A1 and A2 : having

public enum MyEnum {
  A1 = 0, 
  A2 = 0
};

and there's no way to distinguish A1 and A2 to compiler

// if (enumInstance == 0) 
if (enumInstance == MyEnum.A1) 
  ...
// else if (enumInstance == 0) 
else if(enumInstance == MyEnum.A2)
  ... // <- unreachable code

If you have situations like "either A1 or A2 " - the reason why you are trying to assign the same value to both A1 and A2 and you insist on enum , you can try marking the enum with [Flags] attribute

[Flags]
public enum MyEnum {
  None = 0b000, // Neither A1 or A2
  A    = 0b001, // Either A1 or A2
  A1   = 0b011, // Exactly A1  
  A2   = 0b111, // Exactly A2
};

then

if (enumInstance.HasFlag(MyEnum.A)) {
  // Either A1 or A2
}

if (enumInstance == MyEnum.A2) {
  // Exactly A2
}

I found an easy work around that is most suffiecient for my problem.

That is to simply use modulo to allow all members in MyEnum to have different values while still evaluate to the same value.

This method also made it very easy to add enum members to MyEnum and without taking up any extra memory space.

public enum MyEnum {A1 = 0, A2 = 10, A3 = 20, B1 = 1, B2 = 11, B3 = 21,...}
private int evalEnum(MyEnum enumInstance) {
    if(enumInstance == MyEnum.A1) 
        //some logic
    else if(enumInstance == MyEnum.A2)
        //some logic
    else if(enumInstance == MyEnum.B1)
    .
    .
    .
    return ((int) enumInstance) % 10;
} 

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