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 also found the same idea on this question: Two enums have some elements in common, why does this produce an error?
Enum names are in global scope, they need to be unique.
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
).