简体   繁体   English

如何在 C# 中评估两个具有相同值而不相等的枚举成员?

[英]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.我希望成员A1 A2在评估时产生相同的值,但在比较时不应被视为相等。

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 .如果enumInstanceA2类型,我不希望函数输入第一个if语句。 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.我也尝试使用EqualsCompareTo ,但据我了解,只要A1A2分配给相同的值,它们在第一个 if 语句中也会评估为true ( 0 )。

So in conclusion;总而言之; it possible to evaluate A1 and A2 as not equal even if they are assigned the same value?是否可以将A1A2评估为不相等,即使它们被分配了相同的值? 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.枚举只是具有指定名称的 int 值,无法区分 A1 和 A2,因为它们具有相同的实际值。

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.这为您提供了一种从 A1/A2 获得相同值的简单方法,同时仍允许它们进行不相等的比较。

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由于enum实际上是一个整数类型( byteintlong等),您将不得不为A1A2分配不同的值:

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

and there's no way to distinguish A1 and A2 to compiler并且没有办法区分A1A2到编译器

// 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如果您遇到“ A1A2 ”之类的情况 - 您尝试为A1A2分配相同值并且坚持使用enum的原因,您可以尝试使用[Flags]属性标记enum

[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.那就是简单地使用模来允许MyEnum中的所有成员具有不同的值,同时仍然评估为相同的值。

This method also made it very easy to add enum members to MyEnum and without taking up any extra memory space.这种方法还使得将枚举成员添加到MyEnum变得非常容易,并且不会占用任何额外的内存空间。

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;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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