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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:48:12+00:00 2026-05-24T21:48:12+00:00

I have a property of type object , which contains an Enum value, and

  • 0

I have a property of type object, which contains an Enum value, and when I cast it using (int)value, it returns a string of the Enum’s name. Why?

The code where I noticed this is in this answer. Using Convert.ToInt32() correctly casts the Enum to an int, but I was just curious why I would get a string back when using (int). It doesn’t even throw me an error.

Edit

Here’s a quick sample. I commented where I put the breakpoint, and was using the immediate window to determine what the output was.

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public Int32 SomeNumber { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        SomeNumber = 1;
        RootWindow.DataContext = this;

    }
}

public enum MyEnum
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 3
}


/// <summary>
/// Returns true if the int value equals the Enum parameter, otherwise returns false
/// </summary>
public class IsIntEqualEnumParameterConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (parameter == null || value == null) return false;

        if (parameter.GetType().IsEnum && value is int)
        {
            // Breakpoint here
            return (int)parameter == (int)value;
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

MainWindow.xaml

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication5"
        Title="MainWindow" Height="350" Width="525"
        x:Name="RootWindow">

    <Window.Resources>
        <local:IsIntEqualEnumParameterConverter x:Key="IsIntEqualEnumParameterConverter" />
    </Window.Resources>

    <StackPanel>
        <TextBlock Text="{Binding SomeNumber, Converter={StaticResource IsIntEqualEnumParameterConverter}, ConverterParameter={x:Static local:MyEnum.Value1}}" />
    </StackPanel>
</Window>

Edit #2

Just hoping to clear up some confusion…

I said it was returning a string because running ?((int)parameter) in the Immediate Window was returning the enum name, while running ?System.Convert.ToInt32(parameter) was correctly displaying the int.

I found afterwards that it was actually evaluating correctly to the DataTrigger all along. I thought it wasn’t because my control wasn’t visible at runtime, however I discovered that was because of an error in my XAML (I forgot a Grid.Column property, so one control was overlapping another).

Sorry for the confusing question.

Edit #3

Here’s some console app code demonstrating the situation just for Jon 🙂

class Program
{
    static void Main(string[] args)
    {
        object value;
        value = Test.Value1;

        // Put breakpoint here
        // Run ?(int)value vs Convert.ToInt32(value) in the immediate window
        // Why does the first return Value1 while the 2nd returns 1?
        Console.ReadLine();
    }
}

public enum Test
{ 
    Value1 = 1
}
  • 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-24T21:48:12+00:00Added an answer on May 24, 2026 at 9:48 pm

    It sounds like you’re being duped by the Immediate Window. It’s not clear exactly what you did in the Immediate Window, but I can say with absolute certainty that if you cast to an int you do not get a string back. The type system completely prevents that from happening.

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

Sidebar

Related Questions

I have a class with a Property called 'Value' which is of type Object.
I have object A which in turn has a property of type Object B
I have a Word.Field object which is a checkbox and the Type property equals
I have a C++ object, Graph , which contains a property named cat of
Using reflection I have an object which I need to cast into an iterable
I have an object with a Property of type byte[ , ,*] now i'd
I have a object (product), with a property of type 'array' e.g. product.tags =
I have ControlA which accepts an IInterfaceB which has a property of type List<unknownType>
For simplicity have a modelview (MyModelView) that contains a Scheduler property which has a
I have a Model called statistics which has a value field that contains Goals

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.