none
change the value after boxing RRS feed

  • Question

  • when i read the book c# via CLR, see the following code, which describes how to change value after boxing using interface:

    first, declare a interface as below: 

    internal interface IChangeBoxedPoint{

        void Change(Int32 x,Int32 y);

      }

     

    then declare a struct which inherits the above interface:

    internal struct Point: IChangeBoxedPoint

    {

        private Int32 m_x,m_y;

        public Point(Int32 x,Int32 y){

          m_x = x;

          m_y = y;

        }

        public void Change(Int32 x,Int32 y){

          m_x = x;

          m_y = y;

        }

        public override String ToString(){

          return String.Format(“({0},{1})”,m_x,m_y);

        }

      }

    then try to change the boxed value:

    public sealed class Program{

        public static void Main(){

          Point p = new Point(1,1);

          Console.WriteLine(p);

     

          p.Change(2,2);

          Console.WriteLine(p);

     

          Object o = p;

          Console.WriteLine(o);

     

          ((Point)o).Change(3,3);

          Console.WriteLine(o);

                    ((IChangeBoxedPoint)o).Change(5,5);

          Console.WriteLine(o);

        }

      }

    for this code snippet: 

     ((IChangeBoxedPoint)o).Change(5,5);

    it can successfully changed the value to 5,5.

    i cannot understand, can somebody explain the details to me? 

    thanks.

    Friday, December 22, 2017 8:01 AM

Answers

  • Because we have a boxed object of Point  in o and Object  and IChangeBoxedPoint both are reference types and they will be pointing to the same object (memory location), so updating it via calling the interface reference will update the state of object o is holding

    So calling the Change method on the interface will updated the values in the Point object which o is holding as boxed. Here is the exact explanation which is in book to:

    the boxed Point referred to by o is cast to an IChangeBoxedPoint. No boxing
    is necessary here because o is already a boxed Point. Then Change is called, which does change
    the boxed Point’s m_x and m_y fields. The interface method Change has allowed me to change the
    fields in a boxed Point object! Now, when WriteLine is called, it displays (5, 5) as expected.

    The thing to note here is o has got fields updated only not p.

    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    Friday, December 22, 2017 11:00 AM

All replies

  • Because we have a boxed object of Point  in o and Object  and IChangeBoxedPoint both are reference types and they will be pointing to the same object (memory location), so updating it via calling the interface reference will update the state of object o is holding

    So calling the Change method on the interface will updated the values in the Point object which o is holding as boxed. Here is the exact explanation which is in book to:

    the boxed Point referred to by o is cast to an IChangeBoxedPoint. No boxing
    is necessary here because o is already a boxed Point. Then Change is called, which does change
    the boxed Point’s m_x and m_y fields. The interface method Change has allowed me to change the
    fields in a boxed Point object! Now, when WriteLine is called, it displays (5, 5) as expected.

    The thing to note here is o has got fields updated only not p.

    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    Friday, December 22, 2017 11:00 AM
  • If you are interested, the value of boxed structure can be changed even if it does not have the interface. Check the book for unsafe and GCHandle.

    Friday, December 22, 2017 10:37 PM
  • hi ddgr,

    could you please give me more details about your code? yes, it can do the same thing.

    could you please explain the following 2 codes snippet?

    1. 

    dynamic d = (dynamic)o;

    d.change(-55,-55); // which can change the boxed value for o

    2.

    Point t = (dynamic)o;

    t.change(-55,-55); // which cannot change the boxed value for o

    Tuesday, December 26, 2017 2:38 AM