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

  • SEARCH
  • Home
  • 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 4063180
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:41:34+00:00 2026-05-20T15:41:34+00:00

Why is a collection of enum unable to cast to an int? enum Test

  • 0

Why is a collection of enum unable to cast to an int?

enum Test { A = 1, B = 2 };

int? x = (int?)Test.A; // Valid

var collection1 = new[] { Test.A }.Cast<int>().ToList();

// InvalidCastException has thrown (Specified cast is not valid.)    
var collection2 = new[] { Test.A }.Cast<int?>().ToList(); 
  • 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. Editorial Team
    Editorial Team
    2026-05-20T15:41:35+00:00Added an answer on May 20, 2026 at 3:41 pm

    The Cast method can only perform boxing/unboxing conversions, reference conversions, and conversions between an enum type and its underlying integral type. The unboxing has to be to the right type though – it can’t unbox to a nullable type (unlike the C# conversion).

    var collection1 = new[] { Test.A }.Cast<int>()
                                      .Select(x => (int?) x)
                                      .ToList();
    

    For each value, the Cast will unbox from the boxed enum value to the int value, and then the Select will convert the int value to an int? value.

    In this case you can also get away with this:

    var collection1 = new[] { Test.A }.Select(x => (int?) x)
                                      .ToList();
    

    i.e. no Cast step. However, that doesn’t work if you have an object array instead:

    // Fails
    var collection1 = new object[] { Test.A }.Select(x => (int?) x)
                                             .ToList();
    

    You can’t unbox a boxed enum value to a nullable int value. The Cast version still works in that case, however, as it splits the two steps (unboxing first to int, then converting from int to int?.)

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

Sidebar

Related Questions

Under what circumstances is an enum more appropriate than, for example, a Collection that
Is there a more efficient and specialized implementation of a Map collection where Enum
I have a list of pairs (CarType(Enum), code(string)). Unfortunately in this collection codes are
I have a model which has a collection of users. I'm looping through this
I have a collection(people) that has a many-to-many reference to another collection(dogs). Suspend your
I have a collection of items that contain an Enum (TypeCode) and a User
I have a collection of objects like so: public enum ObjectType { Type1, Type2
I have a generic collection (Dictionary), which stores an enum and delegate. So if
Any ideas on how to persist a collection of enums in Grails? Groovy enum:
Consider a collection consisting of enum types. Is there some library method min (or

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.