简体   繁体   English

比较两个不同枚举的最佳方法是什么?

[英]What is the best way to compare two different enums?

I'm thinking what is the best way to compare values from two different enums. 我在想比较两个不同枚举值的最佳方法是什么。

Example: 例:

public enum ExampleEnumA
{
    ExampleValue
}

public enum ExampleEnumB
{
    ExampleValue
}

 if (ExampleEnumA.ExampleValue.ToString() == ExampleEnumB.ExampleValue.ToString())
 {

 }

Comparing strings work, but I know that it's not the most efficient and eligent way. 比较字符串是可行的,但是我知道这不是最有效,最轻松的方法。 How it can be done better? 如何才能做得更好?

EDIT: 编辑:

Maybe it's a design flaw, but it's problem from a real project, not my incorrect understanding of enums. 也许这是一个设计缺陷,但这是一个真实项目的问题,而不是我对枚举的错误理解。 This is how it looks like and there was no time to refactor whole approach. 这就是它的样子,没有时间重构整个方法。

public interface IProvider
{
    Enum SectionType { get; }
}

public class FirstProvider : IProvider
{
    public Enum SectionType
    {
        get { return ExampleEnumA.ExampleValue; }
    }
}

public class SecondProvider : IProvider
{
    public Enum SectionType
    {
        get { return ExampleEnumB.ExampleValue; }
    }
}

public class Program
{
    public void TmpMethod(Enum sectionType)
    {
        var provider = GetFromIoC...

        if (provider.SectionType == sectionType)
        {
            //...
        }
    }
}

Enumerations are like an abstraction layer on top a regular class of integer-based constants. 枚举就像是常规类基于整数的常量之上的抽象层。

That abstraction includes evaluating false even if two enumeraiton values are the same integer but belong to different enumeration types. 即使两个枚举值是相同的整数但属于不同的枚举类型,该抽象也包括对false评估。

What's the best way of comparing two different enumeration types with same underlying value? 比较具有相同基础值的两种不同枚举类型的最佳方法是什么? I would answer that it should be a design flaw if you need to perform this evaluation . 我会回答, 如果您需要执行此评估那应该是设计缺陷

For example, let's say we've implemented these enumerations: 例如,假设我们实现了以下枚举:

public enum States
{
    Open = 1, 
    Closed
}

public enum SpecialFolders 
{
     ProgramFiles86 = 1,
     ProgramFiles64
}

Would make sense something like States.Open == SpecialFolders.ProgramFiles86 ? States.Open == SpecialFolders.ProgramFiles86这样的东西States.Open == SpecialFolders.ProgramFiles86吗? Potentially, they seem to be equal (they won't) because both enumeration values have an underlying value of 1 , but 1 doesn't mean the same if the enumeration type isn't the same. 潜在地,它们似乎是相等的(它们不会),因为两个枚举值的基础值均为1 ,但是如果枚举类型不同,则1并不意味着相同。

It's like saying that... 就像说...

  1. Bread 面包
  2. Meat

...is the same as: ...是相同的:

  1. Steal (Bread == Steal????????????) (面包==偷????????????)
  2. Wood

Maybe... 也许...

...you can defeat the purpose of typing constants as enumerations casting them to int : ...您可以打败将常量键入为int枚举的目的:

if ((int)ExampleEnumA.ExampleValue == (int)ExampleEnumB.ExampleValue)
{

}

...if the underlying type is int . ...如果基础类型为int It could also be long : 也可能long

public enum SomeEnum : long 
{
}

...and you would need to cast left and right part of your evaluation to long , and so on. ...并且您需要将评估的左右部分转换为long ,依此类推。

Anyway, I insist that you shouldn0t go this way. 无论如何,我坚持你不应该这样。 Maybe you should use a regular class of constants instead and you'll avoid 2 casts per evaluation: 也许您应该改用常规的常量类,并且避免每次求值两次强制转换:

public static class States
{
     public const int Open = 1;
     public const int Closed = 2;
}

public static class Materials
{
    public const int Steel = 1;
    public const int Wood = 1;
}

// true! but not that true... I can't understand why these constants equal...
if(States.Open == Materials.Wood)
{
}

BTW, I still consider this a design flaw and you should avoid not using enumerations to workaround a bad design decision. 顺便说一句,我仍然认为这是一个设计缺陷 ,您应该避免不使用枚举来解决错误的设计决策。

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

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