Use the interface to change the fields in the boxed value type.

Source: Internet
Author: User

Use the interface to change the fields in the boxed value type.

Let's take a look at the following code.

Namespace use the interface to change the field of the boxed value type {// 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 ); // replace one or more format items in the specified string with the string format of the specified object} class Program {} static void Main (string [] args ){

Point p = new Point (1, 1 );
Console. WriteLine (p); // display (1, 1)
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 (). p is packed before the first call to writeline, and writeline calls ToString on the packed Point, as expected, the result is (1, 1)

P calls the Change method to Change the value to 2, and packs p again when the writeline is called for the second time, as expected: (2, 2)

Now p needs to be packed for the third time, o will reference the packed Point object, and the third call to writeline will display (2, 2)

Finally, you want to call Change to Change the fields in the boxed Point Object. However, Object o is not informed of the Change method. Therefore, you must first convert o to Point. To convert an o to a Point, you must first unpack the o and bind the fields in the box.

Copy to a temporary Point on the thread stack. The m_x and m_y values of this temporary Point are changed to 3, 3, but the packed Point is not affected by this Change call. Therefore, the fourth display is (2, 2)

 

Use the interface to change fields of the boxed Value Type

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace use the interface to Change the field of the boxed value type {// The interface defines a Change method internal interface IChageBoxedPoint {void Change (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 one or more format items of the specified string to be replaced with the string format of the specified object} class Program {} static void Main (string [] args) {Point p = new Point (1, 1); Console. writeLine (p); // display (1, 1) 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) // pack p, change the packed object, and then discard it; (IChageBoxedPoint) p ). change (4, 4); Console. writeLine (p); // display (2, 2)
// Change the condition of the boxed object to show him (IChageBoxedPoint) o). Change (5, 5); Console. WriteLine (o); // display (5, 5)
}}}

  

The main difference between this code and the above is that the change method

IChageBoxedPoint interface definition. The Point type implements this interface.
// Pack p, change the packed object, and discard it; (IChageBoxedPoint) p ). change (4, 4); Console. writeLine (p); // display (2, 2)
The unpacked Point p is converted to IChangeBoxedPoint. This transformation causes the p value to be boxed, and then Change is called on the boxed value, which indeed changes m_x and m_y to 4 and 4,
However, after the Change is returned, the packed objects are immediately equipped for garbage collection, so the information is displayed)
In the final example, o references the packed Point to be transformed into an IChageBoxedPoint. We do not need to pack here because o is a packed Point. Then call Change, which is correct.
Modify the m_x and m_y fields of the packed Point. The interface method allows you to change fields in a boxed Point object.

Related Article

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.