Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Enum types are distinct, but you're being confused by an implicit cast which is in foreach.</p> <p>Let's rewrite your loop a bit:</p> <pre><code>public static void Main() { List&lt;Cars&gt; cars = new List&lt;Cars&gt;(); List&lt;Animals&gt; animals = new List&lt;Animals&gt;(); cars.Add(Cars.Chevrolet); cars.Add(Cars.Honda); cars.Add(Cars.Toyota); foreach (Cars value in cars) { // This time the cast is explicit. Animals isItACar = (Animals) value; Console.WriteLine(isItACar.ToString()); } Console.ReadLine(); } </code></pre> <p>Now does the result surprise you? Hopefully not, except possibly the fact that you <em>can</em> cast from one enum to another. This is just a more explicit version of what your original code is doing.</p> <p>The fact that there's a cast implicit in every <code>foreach</code> loop (even though it's usually a no-op) is the bit which most developers would find confusing, I think.</p> <p>From section 8.8.4 of the C# 3.0 spec:</p> <blockquote> <p>The above steps, if successful, unambiguously produce a collection type C, enumerator type E and element type T. A foreach statement of the form</p> </blockquote> <pre><code>foreach (V v in x) embedded-statement </code></pre> <blockquote> <p>is then expanded to: </p> </blockquote> <pre><code>{ E e = ((C)(x)).GetEnumerator(); try { V v; while (e.MoveNext()) { v = (V)(T)e.Current; embedded-statement } } finally { ... // Dispose e } } </code></pre> <p>The enumeration conversion itself is covered in section 6.2.2:</p> <blockquote> <p>The explicit enumeration conversions are:</p> </blockquote> <ul> <li>From sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, or decimal to any enum-type.</li> <li>From any enum-type to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, or decimal.</li> <li>From any enum-type to any other enum-type.</li> </ul> <blockquote> <p>An explicit enumeration conversion between two types is processed by treating any participating enum-type as the underlying type of that enum-type, and then performing an implicit or explicit numeric conversion between the resulting types. For example, given an enum-type E with and underlying type of int, a conversion from E to byte is processed as an explicit numeric conversion (§6.2.1) from int to byte, and a conversion from byte to E is processed as an implicit numeric conversion (§6.1.2) from byte to int.</p> </blockquote>
    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