Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the public "value__" field that I can see in Reflector against my enum?

I am looking at an enum I created in Reflector and there is a public integer field called "value__".

What is the purpose of this member?

A link or reference to a document is fine for an answer.

Googling is a pain because "value__" is returning hits for "value".

I have been searching for nearly an hour and only found the links below. Most of these are the same article on different sites. They all show how to access the member via reflection but none of them explain what the member is for.

  • http://powershell.com/cs/forums/p/462/599.aspx

  • http://tfl09.blogspot.com/2008/12/enums-enum-values-and-powershell.html

  • C# function that accepts an Enum item and returns the enum value (not the index)

  • http://www.mail-archive.com/[email protected]/msg02431.html

UPDATE

The last link below discusses (at the bottom) that you can't use value__ as an enum value as it is reserverd but does not say why.

http://www.vijaymukhi.com/documents/books/csadv/chap3.htm

Compiler Error

error CS0076: The enumerator name 'value__' is reserved and cannot be used ... Only for an enum does it not allow us to use the reserved word value__ as it must be using the same word internally to keep track of the enum.

UPDATE 2

The link below is to the MSDN page that for the compiler error that also says "value__" is reserved. But still no joy an finding out what the member does....

http://msdn.microsoft.com/en-us/library/e3988xhs(v=vs.71).aspx

like image 428
J M Avatar asked Mar 06 '11 22:03

J M


1 Answers

The JIT compiler needs a definition of a value type that describes its layout when it gets boxed. Most of them are baked into mscorlib, like System.Int32. The enum keyword lets you create a new value type. The compiler must thus provide a definition for it in the metadata. Which is what you are looking at. You'll see static fields for each enumeration member, used by ToString(). And one instance field name value__ that stores the enumeration value. Key point is that this only exists in the boxed version of an enum value.

like image 89
Hans Passant Avatar answered Oct 04 '22 16:10

Hans Passant