Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 88531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:34:28+00:00 2026-05-10T22:34:28+00:00

Fun with enums in C#. Take one generic list that is created to store

  • 0

Fun with enums in C#. Take one generic list that is created to store some Enum that you had defined previously and add few items in it. Iterate with foreach or GetEnumerator<T>() but specify some other enum then the original and see what happens. I was expecting InvalidCastException or something like that but it perfectly works :).

For the illustration let’s take a simple console application and create two enums there: Cars and Animals:

    public enum Cars     {         Honda = 0,         Toyota = 1,         Chevrolet = 2     }     public enum Animals     {         Dog = 0,         Cat = 1,         Tiger = 2     } 

And do this in main method:

    public static void Main()     {         List<Cars> cars = new List<Cars>();         List<Animals> animals = new List<Animals>();         cars.Add(Cars.Chevrolet);         cars.Add(Cars.Honda);         cars.Add(Cars.Toyota);          foreach (Animals isItACar in cars)         {             Console.WriteLine(isItACar.ToString());         }         Console.ReadLine();     } 

It will print this in console:

Tiger Dog Cat 

Why is this happening? My first guess was that enum is not actually a Type by himself it’s just and int but that’s not true: If we write:

Console.WriteLine(Animals.Tiger.GetType().FullName); We will get his fully qualified name printed! So why this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:34:29+00:00Added an answer on May 10, 2026 at 10:34 pm

    Enum types are distinct, but you’re being confused by an implicit cast which is in foreach.

    Let’s rewrite your loop a bit:

    public static void Main() {     List<Cars> cars = new List<Cars>();     List<Animals> animals = new List<Animals>();     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(); } 

    Now does the result surprise you? Hopefully not, except possibly the fact that you can cast from one enum to another. This is just a more explicit version of what your original code is doing.

    The fact that there’s a cast implicit in every foreach loop (even though it’s usually a no-op) is the bit which most developers would find confusing, I think.

    From section 8.8.4 of the C# 3.0 spec:

    The above steps, if successful, unambiguously produce a collection type C, enumerator type E and element type T. A foreach statement of the form

    foreach (V v in x)  embedded-statement  

    is then expanded to:

    {     E e = ((C)(x)).GetEnumerator();     try {         V v;         while (e.MoveNext()) {             v = (V)(T)e.Current;             embedded-statement         }     }     finally {         ... // Dispose e     } } 

    The enumeration conversion itself is covered in section 6.2.2:

    The explicit enumeration conversions are:

    • From sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, or decimal to any enum-type.
    • From any enum-type to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, or decimal.
    • From any enum-type to any other enum-type.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought that it would be fun to present some classic CS problems and
One of the fun parts of multi-cultural programming is number formats. Americans use 10,000.50
I'm having some fun trying to get my head around some MVP stuf, as
We're having a bit of fun here at work. It all started with one
I'm currently having fun trying to learn some of the Boost libary. I'm currently
just for fun... Is it possible to have a .js file that loads jquery
For fun, I'd like to edit (or add a custom) language pack for my
For fun, I'd like to better understand the building blocks or elements that are
For fun I am trying to see how far I can get at implementing
I am building a fun little app to determine if I should bike to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.