This article is a mirror article of machine translation, please click here to jump to the original article.

View: 31028|Reply: 1

[Source] .ctor and .cctor of .net

[Copy link]
Posted on 4/6/2017 2:56:38 PM | | | |
.ctor is known to everyone

.cctor is not familiar and can be easily misused

The so-called .cctor is a static constructor in C# syntax

Shape like




Similarities and differences between .cctor and .ctor

   1. You can't have access rights identifiers public, private, etc., which can't be used, which is different from .ctor

   2. There must be no return value, which is the same as .ctor

   3. There can be no parameters, .ctor can have multiple parameters

   4. There can only be one at most, and there must be at least one .ctor

   5. The programmer cannot control the timing of the .cctor call, in a sense, the programmer can control the timing of its call

   6. Programmers cannot directly call .cctor, .ctor When the programmer uses the new operator, .ctor will definitely be called


.cctor call timing:


    The static constructor is automatically called before the first instance is created or any static member is referenced. (This is the original quote on MSDN)

    It is clear that .cctor is called only once in the same program domain, rather than being called every time an object is instantiated like .ctor

.cctor use

    Static constructors are used to initialize any static data, or to perform specific operations that only need to be performed once.


For example:

All of the following code is compiled in Windows 7, vs 2005 Release mode, and if the results are different, consider switching to release mode and trying again




Compile this segment of code and then use ILDISM to view it, the method table is as follows



.ctor(): void // This is an instance constructor of the class provided automatically by the compiler

Main():void(string[]) //Program entry point

Obviously, there is no .cctor in the assembly, and the compiler will not automatically provide .cctor functions when a class does not have static fields that need to be initialized, which is different from .ctor, which always exists anyway

Modify the code




Compiling this code, there will be a warning, but that is not important, the important thing is that when viewing the assembly with ILDASTOM, the expected .cctor, static constructor does not appear!


what happened?

Is the inference wrong?. cctor is not a static field used to initialize a class?

Take it easy, please take a look at the code





The only difference between this code and the previous one is that it changes the initial value of the static field i, and the next thing is to witness the magic moment

Let's DASM it, and to our surprise, the much-requested .cctor is finally here!



Look at the .cctor implementation code and you'll see:

.method private hidebysig specialname rtspecialname static
        void  .cctor() cil managed
{
  // Code size       7 (0x7)
  .maxstack  8
  IL_0000: ldc.i4.1 // Instantiate static fields :-)
  IL_0001:  stsfld     int32 only_lonely. A::i
  IL_0006:  ret
} // end of method A::.cctor


Why is this so? The possible reason is that the default value of the Int32 type is 0, and the compiler detects this and optimizes it to not produce .cctor code

reference
https://msdn.microsoft.com/zh-cn/library/k9x6w0hc(VS.80).aspx

Finally, we can implement a .cctor function ourselves to implement special functional requirements

summary
1、. ctor is the construction method;
2、. cctor is a type initializer, which is a static constructor in C#;
3. When class C is instantiated, it will first assign a value to the field that is assigned at the time of declaration, and then call the constructor of the base class, and then construct itself in the same way, all the way to the top-level System.Object, and then come back to execute the code in C's explicit construction method, which is such a recursive process.





Previous:Generic context-bound objects: ContextBoundObject and generic classes are not supported
Next:Summary of the execution order of static, non-static, and constructive methods in a class
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com