Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually your'e already defining a struct... Behind the scenes an enum is just a struct (but which derives from System.Enum) and the values of the enum are defined as constants (you can verify this with ILDASM).</p> <p>Your enum definition translates into the following pseudo C# code:</p> <pre><code>public struct Color : System.Enum { public const int Red = 1; public const int Blue = 1; public const int Green = 1; } </code></pre> <p>The above code won't compile in C# because the compiler doesn't allow defining a struct with an explicit base class, but that's what it emits for an enum definition.</p> <p>Since there is no problem with a type that contains an multiple constants that have the same value, there is no problem with the enum definition.</p> <p>But since the enum does not have unique values you might have an issue when converting into this enum. For example the following two line of codes will return the enum value Red, because the first value is arbitrarily selected. </p> <pre><code>Color color1 = (Color)1; Color color2 = (Color)Enum.Parse(typeof(Color), "1"); </code></pre> <p>Strictly speaking the enum value is not Red, it is 1, but when you print out the value you'll see Red.</p> <p>Also, the following boolean is true which looks a bit weird...</p> <pre><code>// true (Red is Green??) bool b = Color.Red == Color.Green; </code></pre> <p>At the bottom line this is perfectly legal, but it's up to you to use it when it makes sense...</p> <p>Here is a direct link to the section of my .NET tutorial that discusses enumerations under the hood: <a href="http://motti.me/c1E" rel="noreferrer">http://motti.me/c1E</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload