使用 Enum.Parse() 时出现意外结果

发布于 2024-07-13 13:52:44 字数 638 浏览 7 评论 0原文

class Program
{
    static void Main(string[] args)
    {
        String value = "Two";
        Type enumType = typeof(Numbers);
        Numbers number = (Numbers)Enum.Parse(enumType, value);
        Console.WriteLine(Enum.Parse(enumType, value));
    }

    public enum Numbers : int
    {
        One,
        Two,
        Three,
        Four,
        FirstValue = 1
    }
}

这是我在应用程序中使用的枚举的简化版本。 某些枚举名称没有值的原因是因为我使用它们的名称作为参数进行 Enum.Parse 操作,而具有值的枚举名称是从 int 解析的。

如果您单步执行上面的代码并调查“number”变量,您会发现它实际上是“Two”,但控制台中的输出是“FirstValue”。 现在我不明白为什么,你呢?

好吧,解决方案很简单——只需为无价值的枚举赋予一个值即可。 但我还是很好奇。

class Program
{
    static void Main(string[] args)
    {
        String value = "Two";
        Type enumType = typeof(Numbers);
        Numbers number = (Numbers)Enum.Parse(enumType, value);
        Console.WriteLine(Enum.Parse(enumType, value));
    }

    public enum Numbers : int
    {
        One,
        Two,
        Three,
        Four,
        FirstValue = 1
    }
}

This is a simplified version of an enum I use in an application. The reason to why some of the enum names doesn't have a value is because I do Enum.Parse with their names as argument, while the ones with a value is parsed from an int.

If you would step through the code above and investigate the 'number' variable, you would see that it in fact is 'Two', but the output in console is 'FirstValue'. At this point I can't see why, do you?

Okay, the solution is simple - just give the valueless enums a value. But I'm still curious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2024-07-20 13:52:44

我怀疑 FirstValueTwo 的内部值都是 1,因此系统不知道要输出哪个字符串。

public enum Numbers : int
{
    One, // defaults to 0
    Two, // defaults to 1
    Three, // defaults to 2
    Four, // defaults to 3
    FirstValue = 1 // forced to 1
}

每个枚举值都有一个唯一的整数值,但并非每个整数值都有唯一的枚举值。

当您解析“two”时,它会在内部存储为整数1。 然后,当您尝试将其转换回字符串时,根据用于查找该名称的技术,您可能会得到 "Two""FirstValue"。 正如您所说,解决方案是为每个枚举值指定一个定义的整数值。

I suspect that both FirstValue and Two have an internal value of 1, so the system doesn't know which string to output.

public enum Numbers : int
{
    One, // defaults to 0
    Two, // defaults to 1
    Three, // defaults to 2
    Four, // defaults to 3
    FirstValue = 1 // forced to 1
}

There is a unique integer value for every enum value, but there is not a unique enum value for every integer value.

When you parse "two", it gets stored internally as the integer 1. Then when you try and convert it back to a string, depending on the technique used to lookup that name, you could get either "Two" or "FirstValue". As you stated, the solution is to give every enum value a defined integer value.

若相惜即相离 2024-07-20 13:52:44

这是您的问题的一个有趣的转折,请尝试以下枚举...

public enum Numbers : int
{
    One,
    Two,
    Four,
    FirstValue = 1
}

console.WriteLine(...) 现在将打印“Two”!

TwoFirstValue 都表示相同的数字 1,但看到的实际值取决于数字如何转换为其字符串表示形式,反之亦然。

Enum 类使用反射来获取数字的名称,然后将它们存储在数组中,但在执行此操作之前它会对整个事物进行排序。 然后 Enum.ToString() 对排序后的值进行二分搜索以获得字符串表示形式。 由于这样做的方式,您可能会得到不同的结果,具体取决于枚举中元素的数量!

现在,对于 VS 中“看到”的值,我怀疑枚举的调试器可视化工具使用了自己的算法来纠正(?)这个错误。

Here is an interesting twist to your problem, try the following Enum...

public enum Numbers : int
{
    One,
    Two,
    Four,
    FirstValue = 1
}

The console.WriteLine(...) will now print "Two"!

Both Two and FirstValue represent the same number 1 but the actual value seen depends on how the number was converted to its string representation and vice-versa.

The Enum class uses reflection to get the names of the numbers and then stores them in arrays but it sorts the the whole thing before it does so. Then Enum.ToString() does a binary search on the sorted values to get the string representation. Due to the way this is done you may get a different result depending on the number of elements you have in the enumeration!

Now as for the value "seen" in VS I suspect the debugger visualizer for enums uses an algorithm of its own which corrects(?) this bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文