问题描述
$ b
枚举颜色= {红,橙,黄};
enum fruit = {apple,orange,kiwi}; // error:orange is redefined
int kiwi = 42; //错误:猕猴桃被重新定义
我在Ubuntu 14.10中在MonoDevelop中编写C#应用程序,我试图重现这种行为:
使用系统;
命名空间FP
{
class MainClass
{
枚举颜色{红,橙,黄};
enum fruit {apple,orange,kiwi}; // error:orange is redefined
int kiwi = 42; //错误:kiwi被重新定义
public static void Main(string [] args)
{
Console.WriteLine(Hello World!);
}
}
}
上面的程序成功编译。
这是一个已知的MonoDevelop行为还是为什么编译没有错误?枚举值在全局范围内是否唯一?
您正在谈论的是 c
语言。
在 c#
中,枚举不能通过名称全局访问,您始终必须通过 typeName.valueName
,例如 color.Orange
, fruit.Orange
。所以没有冲突。实际上,许多内置枚举使用相同的值名称( StringSplitOptions.None
, CompareOptions.None
)。 p>
I read in The Fundamentals of Programming course that the enum
values should be unique, having the following example:
enum color = { red, orange, yellow };
enum fruit = { apple, orange, kiwi}; // error: orange is redefined
int kiwi = 42; // error: kiwi is redefined
I write C# apps in MonoDevelop on Ubuntu 14.10 and I tried to reproduce this behavior:
using System;
namespace FP
{
class MainClass
{
enum color { red, orange, yellow };
enum fruit { apple, orange, kiwi}; // error: orange is redefined
int kiwi = 42; // error: kiwi is redefined
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
}
}
}
The program above compiled successfully.
Is this a known MonoDevelop behavior or why it was compiled without errors? Should the Enum values be unique in the global scope?
What you are talking about is behaviour of the c
language.
In c#
enums are not globally accessible by name, you always have to access them by typeName.valueName
, e.g. color.Orange
, fruit.Orange
. So there will be no conflict. In fact many in-built enums use the same value names (StringSplitOptions.None
, CompareOptions.None
).
这篇关于枚举值在全球范围内是否独一无二?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!