To change a field in a boxed value type by using an interface

Source: Internet
Author: User

Let's look at the following section of code

namespace using an interface to change the boxed value type of a field {     //point is a value type    internal struct point    {        private Int32 m_x, m_y;        Public point (Int32 x, Int32 y)        {            m_x = x;            m_y = y;        }        public void change (int x, int y)        {            m_x = x;            m_y = y;        }        public override string ToString ()        {            return string. Format ("({0},{1})", m_x, m_y);            Fromat returns the string format of one or more format items in the specified string replaced by the specified object.        class program    {               }        static void Main (string[] args)        {

Point P = new Point (1, 1);
Console.WriteLine (P);//display (+/-)
P.change (2, 2);
Console.WriteLine (P);//display (2,2)
Object o = P;
Console.WriteLine (o);//display (2,2)
(point) O). Change (3, 3);
Console.WriteLine (o);//display (2,2)
Console.readkey ();

        }    }}

Main creates a point value type object on the stack and assigns a value (WriteLine), and the P is boxed before the first call to the WriteLine, and the ToString is raised on the bin, as expected, as

P calls the Change method, changing the value to 2, and then boxing the P again when the second call to WriteLine, as expected (2,2)

Now P is going to be boxed for the third time, O will refer to the boxed point object, and the third call to WriteLine will be displayed (2,2)

Finally, you want to call change to make changes to the fields in the boxed point object, but object o is not aware of the changing method, so turn O into point first. Convert O to point to first unpacking and the boxed field

Copied to a temporary point on the thread stack. The m_x,m_y of this temporary point becomes 3, 3, but the boxed point is not affected by the change call, so the fourth time (2,2)

To change the field of a boxed value type with an interface

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; namespace using an interface to change the boxed value type of a field {//interface define a change method internal interface Ichageboxedpoint {void changes (int x, int y    );        }//point is a value type internal struct Point:ichageboxedpoint {private Int32 m_x, m_y;            Public point (Int32 x, Int32 y) {m_x = x;        m_y = y;            } public void change (int x, int y) {m_x = x;        m_y = y; } public override string ToString () {return string.            Format ("({0},{1})", m_x, m_y); Fromat returns the string format of one or more format items in the specified string replaced by the specified object. Class Program {} static void Main (string[]            args) {point P = new Point (1, 1);            Console.WriteLine (P);//Display (P.change) (2, 2);            Console.WriteLine (P);//Display (2,2) object o = P; Console.WriteLine (o);//display (2,2)            (point) O).            Change (3, 3); Console.WriteLine (o);//display (2,2)//P Boxing, change the boxed object, and discard it; ((Ichageboxedpoint) p).            Change (4, 4); Console.WriteLine (P);//display (2,2)
Change the boxed object The disease shows him ((Ichageboxedpoint) o). Change (5,5); Console.WriteLine (o);//display (5,5)
} }}

  

The main difference between this code and the top is that the change method is determined by the

Ichageboxedpoint interface definition, the point type implements this interface.
  Boxing p, changing the boxed object, and discarding it;            ((Ichageboxedpoint) p). Change (4, 4);            Console.WriteLine (P);//display (2,2)
Here the unboxed point P is transformed into Ichangeboxedpoint, which causes the value of p to be boxed, and then the change is called on the boxed value, which really makes M_x,m_y 4, 4,
However, after the change is returned, the boxed object is immediately equipped for garbage collection, so the display (2,2)
In the final example, o refers to the boxed point to be transformed into a ichageboxedpoint. Boxing is not required here, because O is already a boxed point. And then call change, he can correctly
Modifies the m_x,m_y field of a boxed point. The interface method allows you to change the fields in a boxed point object.

To change a field in a boxed value type by using an interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.