为什么没关系枚举有两个不同的名称以相同的数值? [英] Why is it okay for an enum to have two different names with the same numeric value?

查看:164
本文介绍了为什么没关系枚举有两个不同的名称以相同的数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了一个细微的错误在那里我有一个枚举有两个名字unintentially共享相同的数值(在这种情况下,红色= 10和深红色= 10)。我有点惊讶,这不是一个语法错误。

 公共枚举颜色
{
红= 10,
蓝= 11,
绿= 12,
深红= 10
}
// Debug.Write(Colour.Red == Colour.Crimson )输出真正的

是否有任何真实世界之所以出现这种行为可能是一个有用的还是认为应该是一个语法错误?


解决方案

 公共枚举颜色
{
红色= 10,
高棉= 10,
蓝= 11,
布鲁= 11,
绿= 12,
垂直= 12,
黑色= 13,
比诺= 13
}


I just discovered a subtle bug where I had an enum with two names unintentially sharing the same numeric value (in this case red=10 and crimson=10). I'm a bit surprised this isn't a syntax error.

public enum Colour
{
    Red=10,
    Blue=11,
    Green=12,
    Crimson=10
}
// Debug.Write(Colour.Red==Colour.Crimson) outputs True

Is there any real world reason why this behaviour might be a useful or do think it should be a syntax error?

解决方案

public enum Colour
{
    Red=10,
    Rouge=10,
    Blue=11,
    Bleu=11,
    Green=12,
    Vert=12,
    Black=13,
    Noir=13
}

这篇关于为什么没关系枚举有两个不同的名称以相同的数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆