none
Can Casting an Object Change an Object's Reference? RRS feed

  • Question

  • I'm having an issue where a reference to an object is not being preserved, and I'm trying to figure out what the issue is. I could override Equals(), but I'd like to understand this.

    I have a read-only object that after going through a series of casts loses equality with the original. Basically, the flow is like this:

    public readonly Dog MY_ORIGINAL_DOG = new Dog();
    ...
    myOrigDogRadioButton = new RadioButton { Content = MY_ORIGINAL_DOG };
    ...
    var selectedDog = (Dog)myOrigDogRadioButton.Content
    ...
    if (selectedDog == MY_ORIGINAL_DOG)...   <== returns false

    From my thinking, this should return true because it's referencing the same object, however, it appears that the reference is being changed through casting. Is this true?

    Saturday, August 11, 2018 11:31 AM

Answers

  • Hi Matt.Brown,

    Thank you for posting here.

    >>From my thinking, this should return true because it's referencing the same object, however, it appears that the reference is being changed through casting. 

    According to your description, I made a demo, and the result of code returned true. This verifies your guess.

    using System;
    namespace Can_Casting_an_Object_Change_an_Object_s_Reference
    {
        class Dog
        { }
        class Program
        {
            public readonly Dog MY_ORIGINAL_DOG = new Dog();
            static void Main(string[] args)
            {
                Program program = new Program();
                Console.WriteLine(program.isEquals());
            }
            public bool isEquals()
            {
                var myOrigDogRadioButton = new { Content = MY_ORIGINAL_DOG };
                var selectedDog = (Dog)myOrigDogRadioButton.Content;
                if (selectedDog.Equals(MY_ORIGINAL_DOG))
                    return true;
                return false;
            }
        }
    }

    However, your result is false. Therefore, I suggest you check whether the SelectedDog reference is changed in your code. If possible, please provide more information.

    Best Regards,

    JianGuo


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Matt.Brown Monday, August 13, 2018 12:40 PM
    Monday, August 13, 2018 8:04 AM

All replies

  • hmm, you're right. This should normally work. E.g.

    namespace ConsoleCS
    {
        using System;
    
        public class Container
        {
            public object Content { get; set; }
        }
    
        public class Dummy
        {
        }
    
        public class Program
        {
            private readonly Dummy dummy = new Dummy();
    
            public static void Main(string[] args)
            {
                Program program = new Program();
                program.TestReference();
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            private void TestReference()
            {
                Container container = new Container() { Content = this.dummy };
                var contentUncasted = container.Content;
                var contentCasted = (Dummy)container.Content;
                Console.WriteLine("Reference (direct access) is " + (container.Content == this.dummy ? "equal." : "not equal."));
                Console.WriteLine("Reference (uncasted var) is " + (contentUncasted == this.dummy ? "equal." : "not equal."));
                Console.WriteLine("Reference (casted var) is " + (contentCasted == this.dummy ? "equal." : "not equal."));
            }
        }
    }
    

    I cannot reproduce your sample, cause WinForms' RadioButton has no Content property. What class is this or what UI framework do you use? 

    Sunday, August 12, 2018 12:46 PM
  • Hi Matt.Brown,

    Thank you for posting here.

    >>From my thinking, this should return true because it's referencing the same object, however, it appears that the reference is being changed through casting. 

    According to your description, I made a demo, and the result of code returned true. This verifies your guess.

    using System;
    namespace Can_Casting_an_Object_Change_an_Object_s_Reference
    {
        class Dog
        { }
        class Program
        {
            public readonly Dog MY_ORIGINAL_DOG = new Dog();
            static void Main(string[] args)
            {
                Program program = new Program();
                Console.WriteLine(program.isEquals());
            }
            public bool isEquals()
            {
                var myOrigDogRadioButton = new { Content = MY_ORIGINAL_DOG };
                var selectedDog = (Dog)myOrigDogRadioButton.Content;
                if (selectedDog.Equals(MY_ORIGINAL_DOG))
                    return true;
                return false;
            }
        }
    }

    However, your result is false. Therefore, I suggest you check whether the SelectedDog reference is changed in your code. If possible, please provide more information.

    Best Regards,

    JianGuo


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Matt.Brown Monday, August 13, 2018 12:40 PM
    Monday, August 13, 2018 8:04 AM
  • Thanks for the reply. It's WPF.
    Monday, August 13, 2018 10:32 AM
  • Thanks for taking the time to test it. I'm going to have to step through my code to see what's going on. 

    One thing that's different in my actual code is that the object is added to List(s), however, adding an object to a List should be by reference, right?

    Is there a way to identify an object's memory reference while debugging?

    Thanks again.

    Monday, August 13, 2018 10:36 AM
  • The issue was that I should have made the readonly object static for the particular case I was using it for. Multiple instances of the object were created since it belonged to each instance and not the class.

    Definitely an oversight on my part.

    Thanks again for the help.

    Monday, August 13, 2018 12:42 PM
  • I would be helpful for others to see a concise sample show this problem.
    Tuesday, August 14, 2018 7:02 PM