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 8484965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:30:56+00:00 2026-06-10T20:30:56+00:00

I have an enum, Foo : public enum Foo { Alpha, Bravo, Charlie }

  • 0

I have an enum, Foo:

public enum Foo { Alpha, Bravo, Charlie }

If I attempt the following cast from a boxed int to a Foo?, I get an InvalidCastException:

var x = (Foo?)(object)1;

This led me to some experimentation…

var x = (Foo)(object)1; // succeeds
var x = (long)(object)1; // fails
var x = (long?)(object)1; // fails
var x = (long)1; // succeeds
var x = (long?)1; // succeeds
var x = (int)(object)1; // succeeds
var x = (int?)(object)1; // succeeds

What this tells me is that you can cast from a boxed int to an enum but not to a long, and you cannot convert from a boxed int to any kind of nullable except an int?.

By the way, the reason I’m casting the int to object first is that I’m really trying to cast from an int to a generic parameter TValue, like this:

var x = (TValue)(object)1;

If I didn’t have (object), it wouldn’t compile. (See this blog post by Eric Lippert for details.)

Questions

  1. Why can you convert from a boxed int to an enum, but not to a nullable enum (and not to a long nor a long?)?

  2. What’s the easiest way to rewrite var x = (TValue)(object)1; so that it compiles, works at runtime, and is performant (assuming TValue is determined to be a Foo? at runtime)?

  • 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-06-10T20:30:58+00:00Added an answer on June 10, 2026 at 8:30 pm

    To answer the first question, you can convert from a boxed value to an enum only if the boxed value is of the enum’s underlying type. Had you declared

    enum Foo : byte { ...
    

    you would not be able to cast from boxed int to Foo.

    To answer the second question, try

    var x = (TValue)Enum.ToObject(typeof(TValue), 1);
    

    This involves boxing, though; if you need a solution that won’t box, it will be more complicated.

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

Sidebar

Related Questions

I have the following model : public class Foo { [Key] public int FooID
I have the following enum definition: public enum MyEnumeration{ ENUM_ONE(1,foo), ENUM_TWO(2,bar); private String code;
Lets say you have this enum: Public Enum ApplicationStatus <Foo(Active)> Active = 1 <Foo(Inactive)>
I have classes public class FooKeyedCollection : KeyedCollection<FooType,Foo> { } public enum FooType {
Say I have the following: Foo::Foo() { value = 25; //default constructor... } Foo::Foo(Enum
Let's say I have an interface: public interface Foo{ String bar(int baz); } Now
The following code: foo.h #include bar.h class foo{ public: enum my_enum_type { ONE, TWO,
Imagine we have an enum: enum Foo { A=1,B=2,C=3 } If the type is
If I have an C++ enum: enum Foo { Bar, Baz, Bork, }; How
I have code: #include <cstdio> template<template<typename...> class> struct Foo { enum { n =

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.