Learning CLR via C # (3)

Source: Internet
Author: User
  1. CLR requires that the interface methods be marked as virtual. If the methods are not explicitly marked as virtual in the source code, the compiler will mark them as virtual and sealed, which will prevent the derived classes from overwriting the interface methods.
  2. When a type is loaded into CLR, a method table is created and initialized for this type. In this method table, each new method introduced by the type has a corresponding record item. In addition, a record item is added for all the virtual methods inherited by the type.
  3. You can use the display interface (eimi) to enhance the type security during compilation:
internal struct SomeValueType : IComparable{    private int _value;    public SomeValueType(int value) { _value = value; }    public int CompareTo(SomeValueType other)    {        return _value - other._value;    }    int IComparable.CompareTo(object other)    {        return CompareTo((SomeValueType)other);    }}
  1. CLR treats all multidimensional arrays as non-zero-base arrays. Accessing a one-dimensional 0-base array is a little faster than accessing a non-zero-Base One-dimensional array or multi-dimensional array.
  2. Garbage collection:
public void Test(){    Timer t = new Timer(TimerCallBack, null, 0, 2000);    Console.ReadLine();}private void TimerCallBack(object state){    Console.WriteLine("In TimerCallBack: " + DateTime.Now.ToString());    GC.Collect();}

Timer only runs once.

 

public void Test(){    using (Timer t = new Timer(TimerCallBack, null, 0, 2000))    {        Console.ReadLine();    }}

It can be correctly executed multiple times.

  • If a type defines the Finalize method, a pointer to this object is put into an end list before the instance constructor of this type is called. When garbage collection starts, the garbage collector scans the final list to find the pointer of the garbage object. If it finds the pointer, it removes it from the final list and appends It To The freachable list. A special high-priority CLR thread is responsible for calling the Finalize method. The object pointed to by the pointer added to freachable will not be recycled until the Finalize method is called.
  • The newly created objects in GC belong to the 0th generation. The surviving objects in the 0th generation recycle are upgraded to the 1st generation, and the surviving objects in the 1st generation recycle are upgraded to the 2nd generation. The higher the generation, the larger the memory budget allocated, and the lower the recovery frequency.
  • Cross-appdomain communication method: 1. Send messages by reference (inherit marshalbyrefobject ). 2. Send messages by value (serializable ).
  • For reflection calls with parameters such as REF and out:
internal sealed class SomeType{    public void RefMethod(ref int a)    {        a += 3;    }}

Available

typeof(int).MakeByRefType()

Or

Type.GetType("System.Int32&")

To obtain the type information.

 

public void Test(){    object[] args = new object[] { 12 };    Type t = typeof(SomeType);    MethodInfo method =        t.GetMethod("RefMethod", new[] { typeof(int).MakeByRefType() });    method.Invoke(new SomeType(), args);    Console.WriteLine(args[0]);    method =        t.GetMethod("RefMethod", new[] { Type.GetType("System.Int32&") });    method.Invoke(new SomeType(), args);    Console.WriteLine(args[0]);}

The calling method of out and ref is the same, because there is no difference between the two at the Il level.

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.