Small details of the Microsoft Common Language runtime

Source: Internet
Author: User

1.

In fact, when we use string concatenation, it is a string that calls his static method Concat,concat accepts the object parameter, which means that if it is a reference type, it can be passed directly into the value type. Like what

Int a = 0;

Object b =a;

Console.WriteLine (A + "," + (int) b);

This allows for a more unboxing and boxing operation, which reduces efficiency.

The correct code should be:

Console.WriteLine (a.tostring () + "," +b);

This does not cause any boxing, unpacking operations.

2.

WriteLine provides a number of overloaded methods, which means that any single method you pass in will not cause a unboxing, boxing operation:

Int a = 0;

Console.WriteLine (a);//does not cause a unboxing, boxing operation, it calls the WriteLine (Int32)

If you really want to pass in more than one parameter, you can use the WriteLine match:

Int a=0,b=1,c=2;

Console.WriteLine ("{0},{1},{2}", a,b,c);

This also does not cause unpacking, boxing operations.

3.

The ToString method returns a reference type, so does a value type call cause boxing? The answer is no.

As long as we rewrite the ToString method, it does not cause boxing, usually, in order to call a virtual method, the CLR will recognize its type by invoking the "type variable value pointer", but the value type does not have this thing, so it is bound to cause a boxing, but if the user overrides, The CLR then invokes it in a non-virtual way, so it does not cause a boxing operation.

The call to the GetType method must be boxed, because this is inherited from the object class, and the virtual method must call the type variable value pointer recognition type.

All in all, the trick to determining whether a method passes in a parameter will cause a boxing operation is to see what type of argument the method accepts (nonsense).

4.

Look at the following code, point (with X, y two fields) is a user-defined struct, he inherits the Ichangeboxedpoint interface, the interface has a change (modify the value of x, Y) method:

Point P = new Point (in);

((Ichangeboxedpoint) p). Change (bis);

So what is the value of x, y inside p? The answer is 1, 1. The reason is that the CLR first allocates space for p on the stack, and then the P is boxed, which is a copy of the instance of P to the managed heap, but! The change method is reclaimed by the garbage collection mechanism only after the P instance on the managed heap has been modified.

5.

The identity and equality of objects, because of the comparison of reference objects there are two ways, one is to see if they point to the same object, the other is whether the field is equal, for the first comparison, you should use the static method of object ReferenceEquals instead of the = = operator or the Equals Method!

If you want to get a safe comparison operation you can inherit the IEquatable interface and overload ==/! = operator.

Also, if you implement the Equals method, implement the GetHashCode method and implement a good hashing algorithm.

6.

Covariance and variation:

    1. The type parameter has an out keyword before it can receive subtypes.
    2. The type parameter has the IN keyword before it can accept the parent type.

Assume that the child class derives from the parent class.

Like List<in t> We can write this:

list<child> L1 =new ilist<parent> ();

If it is list<out t> can write this:

list<parent> L2 =new ilist<child> ();

However, to ensure the readability of your code, use covariance and variation only when necessary.

Small details of the Microsoft Common Language runtime

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.