gpt4 book ai didi

c# - 具有重复值的枚举的 GetName

转载 作者:可可西里 更新时间:2023-11-01 08:39:50 25 4
gpt4 key购买 nike

如果我在 C# 枚举中有重复值,说

enum MyE {
value1 = 1,
value2 = 2,
valued = 1
}

以下字符串的值应该是什么?

MyE N = (MyE)1;
string V1 = N.ToString();
string V2 = GetName(MyE, 1);

V1 和 V2 必须包含相同的值是真的吗?这些值应该是什么?

我没有在 MSDN 或这里找到任何关于重复枚举的“解引用”,如果我错过了,请给我指向一个链接。

最佳答案

实验表明:

V1 = "value1"

V2 = "value1"

但是,这并不能保证。 MSDN page on Enum.GetName状态:

If multiple enumeration members have the same underlying value, the GetName method guarantees that it will return the name of one of those enumeration members. However, it does not guarantee that it will always return the name of the same enumeration member. As a result, when multiple enumeration members have the same value, your application code should never depend on the method returning a particular member's name.

关于c# - 具有重复值的枚举的 GetName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9754604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com