Search code examples
c#.netvb.net.net-4.0enums

Why [Enum].Parse has an ignoreCase parameter?


Since an Enumeration in VB.Net cannot contains duplicated values like this:

Enum Test
  A
  a
End Enum

Then why the [Enum].Parse method has a StringCase parameter?

[Enum].Parse(GetType(Enum), Value, Ignorecase)

http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx

Parse(Type, String, Boolean)

It really has any sense for me.

Which with logic developers of .NET Framework Classes has written that ignorecase flag in that method?

I think that the [Enum].Parse logic should be to check automatically for ignorecase value instead of passing a boolean parameter to the method because an enum cannot contains duplicates ...or it can contains?


Solution

  • An enum can contain values which differ only in case - you just can't declare them in VB. This is perfectly valid C#:

    public enum Foo
    {
        A, a;
    }
    

    Additionally, even if the enum couldn't contain values differing only in case, that wouldn't mean you'd either always want it to be case-sensitive or always want it to be case-insensitive. Your suggest that it should always be case-insensitive would be annoying for situations where you were trying to provide a case-sensitive match. It's very rarely a good idea to make case-insensitive matching the only option in an API, IMO.