Basic knowledge of C # basics (19) Boxing and unpacking of value types (ii)

Source: Internet
Author: User
If the code causes the compiler to be boxed repeatedly, it can be changed to manual boxing, so that the code executes faster, see the following code:

            Manual boxing            Int32 v = 5;            Because of string. The parameter of format is type object, so this will cause three boxing.            Console.WriteLine (String. Format ("{0},{1},{2}", V, V, v));            Modification, of course, this is only a small trick, such as the program in the same value of the same operation executed multiple times,            //should be executed first, and then used.            Object o = v;//packing once            Console.WriteLine (String. Format ("{0},{1},{2}", O, O, O));

With the previous code snippet, it's easy to tell when the value type is boxed when we write the program again. It's all about boxing when you want to get a reference to a value type. It is also clear that there is a difference between a value type and a reference type:
1, the value type does not allocate space in the managed heap, and the reference type, after instantiation, allocates space for the members specified in the class on the heap.
2, the value type does not have an extra member of the object on the heap, that is, type object pointer, synchronous index. The
unboxed value type does not have a synchronous index, so it is not possible to have multiple threads synchronize access to this instance using a method of that type's class, such as lock.
Although an unboxed value type does not have a type object pointer, it can still invoke a virtual method inherited or overridden by a type, such as equals,gethashcode,tostring. If a value type overrides any of these virtual methods, the CLR can call the method non-virtual because the value type is implicitly sealed and no type can derive from it. In addition, instances of value types used to call virtual methods are not boxed. If the overridden virtual method is to invoke the implementation of the method in the base class, the value type instance is boxed when the implementation of the base class is called. Because these methods are System.Object defined, these methods expect the this argument to be a pointer to an object on the heap.
In addition, transforming an unboxed instance of a value type to an interface of a type requires the instance to be boxed. Because an interface variable must contain a reference to an object on the heap. Look at the following code:

 Class Program {static void Main (string[] args) {point p1 = new Point (10, 10);            Point P2 = new Point (20, 20); Called ToString is not boxed, here ToString is a virtual method Console.WriteLine (P1.            ToString ()); GetType is a non-virtual method, p1 to be boxed Console.WriteLine (P1.            GetType ()); This is called the public int CompareTo (point P)//P2 is not boxed Console.WriteLine (P1.            CompareTo (p2));            P1 is to be boxed, which is when the unboxed value type is converted to an interface of type IComparable c = p1;            Console.WriteLine (C.gettype ()); This is called Public Int32 CompareTo (Object o),//and C is a reference, so it is not boxed Console.WriteLine (P1.            CompareTo (c));            Here is called the C CompareTo method, the parameter is the object type//So to P2 boxing Console.WriteLine (C.compareto (p2));            Remove the box to C, and copy the value to P2 in P2 = (point) C; Console.WriteLine (P2.        ToString ());        }} internal struct point:icomparable {private Int32 x; Private Int32 Y            Public point (Int32 x, Int32 y) {this.x = x;        This.y = y; } public override string ToString () {return string.        Format ("{0},{1}", X, y);//Boxing here two times, do not know there is no good way.  } public int CompareTo (point p) {return math.sign (math.sqrt (x * x + y * y)-math.sqrt (p.x * p.x        + p.y * p.y)); } public Int32 CompareTo (Object o) {if (GetType ()! = O.gettype ()) {th            Row New ArgumentException ("O is isn't point.");        } return CompareTo ((point) O); }    }

The above is the basic knowledge of C # basics (19) value type of boxing and unpacking (ii) of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.