Learning CLR via C # (1)

Source: Internet
Author: User

Take notes. Do not omit things that you think are important or do not know before, and urge yourself to learn them.


  1. The is and as operators both verify that the object is compatible with the specified type, and the strong conversion also checks. Therefore, the best combination is to judge whether it is null after the AS, rather than the post-as or is forced conversion. The former will only have one check, and the latter will have two checks.
  2. A namespace is a language feature such as C #. CLR does not know anything about a namespace.
  3. The type object stores the type object pointer, Synchronous Index block, static fields and methods (static, virtual, non-virtual ). During method execution, JIT compilation is performed first (if needed), and then the Code Compiled by JIT is called.
  4. The JIT compiler generates different CPU commands (such as x86 and x64) based on the environment. Therefore, the performance of the method is greatly reduced during the first execution, which will be significantly improved later.
  5. Long is 64-bit in C #, but not in some other languages of CLR, such as (c ++/CLI). Therefore, the author tends to use int32 instead of Int, int64 replaces long. But I still get used to and like the latter :)
  6. When checked is used, overflow is detected. If unchecked is used, no overflow is detected. It is not detected by default. The corresponding il commands are Add. ovf (detection) and add (not detection ). Other commands are similar.
  7. Only the reference type can be locked, and the value type is not allowed, because the reference type has a synchronized index block (Sync block index ). Of course, the boxed value type can be locked because it is already a reference type.
  8. The packing cost is large, because there is a copy process, and the unpacking cost is relatively small, it is just a process in which reference points to change. However, unpacking is usually accompanied by a copy.
  9. The following code:
  10. int a = 5;Console.WriteLine("{0}, {1}, {2}", a, a, a);

    It is packed three times to generate three identical objects in the heap, wasting time and memory. In this case, first pack a and then pass the referenced object as a parameter, or a. tostring and then use the string as a parameter.

  11. Use the value type with caution unless it meets the following requirements: 1. It is relatively small because there will be replication overhead during method transfer. 2. Immutable. Otherwise, unexpected errors may occur during binning and changing the value.
  1. The introduction to dynamic is relatively simple, so you have time to study the knowledge of DLR.
  2. Internal is accessible to other assembly! That is, the legendary friend assembly can be implemented by adding the internalsvisibleto tag.
  3. Static classes cannot be instantiated. The generated il does not have the. ctor method. Static classes must be derived directly from system. Object and cannot implement any interfaces (interface methods can only be provided to instance calls). They cannot have instance members or be used as instance variables.
  4. Declaring a static class causes the C # compiler to declare the class as abstract and sealed.
  5. The difference between call and callvirt: the latter cannot be used in static methods. The latter has an operation to check whether the object is null.
static void Test(){    Program p = null;    int a = p.GetFive();}public int GetFive(){    return 5;}

Can I run the test method if I change callvirt of P to call?

  1. Reflection can modify the readonly field! Omnipotent reflection. In the face of reflection, the Shenma modifier is a cloud, and the law can no longer stop it.
  2. The memberwiseclone method of the object will create a new type of instance without calling the instance constructor. The Mechanism is to directly copy the bytes of the source object to the new object after the memory is allocated, it's almost no wonder that it is a small copy.
  3. The Type constructor (static constructor) is secure for automatic thread synchronization and is suitable for initializing any objects that require Singleton in the Type constructor.
  4. The Type constructor is. cctor (class constructor ).
  5. Custom display conversion or implicit conversion operators cannot be called through is.
  6. The compiler will first look for the list of methods defined by the class itself, and then find the extension method. Therefore, there may be version issues with the extension method.
  7. I was wondering why the extension methods in Microsoft's class library should judge whether this is null. It is actually a syntactic sugar, just like a normal static method.
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.