Code expansion of Constructor

Source: Internet
Author: User
Tags mscorlib

1. Class 1

Public class
{
Private Int32 x = 15;
}

Check its default non-argument Constructor's IL Code:

.method public hidebysig specialname rtspecialname         instance void  .ctor() cil managed{  // Code size       16 (0x10)  .maxstack  8  IL_0000:  ldarg.0  IL_0001:  ldc.i4.s   15  IL_0003:  stfld      int32 ConsoleApplication9.A::x  IL_0008:  ldarg.0  IL_0009:  call       instance void [mscorlib]System.Object::.ctor()  IL_000e:  nop  IL_000f:  ret} // end of method A::.ctor

---- Assign 15 to the variable x (initialization), and then call the non-argument constructor in the parent class Object

2. Category 2

Public class MyClass
{
Private String name = "josh ";
Private Int32 x = 15;

Public MyClass (String sname)
{
This. name = sname;
}

Public MyClass (Int32 sx)
{
This. x = sx;
}
}

There are two overloaded constructors in this class. view the IL Code formed by these two constructors respectively:

Constructor IL code with String type parameters:

.method public hidebysig specialname rtspecialname         instance void  .ctor(string sname) cil managed{  // Code size       36 (0x24)  .maxstack  8  IL_0000:  ldarg.0  IL_0001:  ldstr      "josh"  IL_0006:  stfld      string ConsoleApplication9.MyClass::name  IL_000b:  ldarg.0  IL_000c:  ldc.i4.s   15  IL_000e:  stfld      int32 ConsoleApplication9.MyClass::x  IL_0013:  ldarg.0  IL_0014:  call       instance void [mscorlib]System.Object::.ctor()  IL_0019:  nop  IL_001a:  nop  IL_001b:  ldarg.0  IL_001c:  ldarg.1  IL_001d:  stfld      string ConsoleApplication9.MyClass::name  IL_0022:  nop  IL_0023:  ret} // end of method MyClass::.ctor

Constructor IL code with Int32 type parameters:

.method public hidebysig specialname rtspecialname         instance void  .ctor(int32 sx) cil managed{  // Code size       36 (0x24)  .maxstack  8  IL_0000:  ldarg.0  IL_0001:  ldstr      "josh"  IL_0006:  stfld      string ConsoleApplication9.MyClass::name  IL_000b:  ldarg.0  IL_000c:  ldc.i4.s   15  IL_000e:  stfld      int32 ConsoleApplication9.MyClass::x  IL_0013:  ldarg.0  IL_0014:  call       instance void [mscorlib]System.Object::.ctor()  IL_0019:  nop  IL_001a:  nop  IL_001b:  ldarg.0  IL_001c:  ldarg.1  IL_001d:  stfld      int32 ConsoleApplication9.MyClass::x  IL_0022:  nop  IL_0023:  ret} // end of method MyClass::.ctor

---- It is not difficult to find that both constructors form the same IL code to initialize instance variables, that is, initialize repeated operations. If one class contains multiple such overloaded constructors, this may cause code expansion problems.

3. Category 3 (Method Improvement)

Public class TestClass
{
Private String name;
Private Int32 x;

Public TestClass ()
{
Name = "josh ";
X = 15;
}

Public TestClass (String sname)
: This ()
{
Name = sname;
}

Public TestClass (Int32 sx)
: This ()
{
X = sx;
}
}

---- When defining instance variables, only do not initialize them. A constructor initializes them. Other constructor only needs to call this constructor.

View IL Code:

A. No parameter Constructor. Method public hidebysig specialname rtspecialname instance void. ctor () cel managed {// Code size 29 (0x1d ). maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib] System. object ::. ctor () IL_0006: nop IL_0007: nop IL_0008: ldarg.0 IL_0009: ldstr "josh" IL_000e: stfld string ConsoleApplication9.TestClass: name IL_0013: ldarg.0 IL_0014: ldc. i4.s 15 IL_0016: st1_int32 ConsoleApplication9.TestClass: x IL_001b: nop IL_001c: ret} // end of method TestClass ::. ctorB. constructor with String Parameters. Method public hidebysig specialname rtspecialname instance void. ctor (string sname) cel managed {// Code size 17 (0x11 ). maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void leleapplication9.testclass ::. ctor () IL_0006: nop IL_0007: nop IL_0008: ldarg.0 IL_0009: ldarg.1 IL_000a: stfld string Syntax: name IL_000f: nop IL_0010: ret} // end of testmethod class ::. ctorC. constructor with Int32 Parameters. Method public hidebysig specialname rtspecialname instance void. ctor (int32 sx) cel managed {// Code size 17 (0x11 ). maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void leleapplication9.testclass ::. ctor () IL_0006: nop IL_0007: nop IL_0008: ldarg.0 IL_0009: ldarg.1 IL_000a: st1_int32 failed: x IL_000f: nop IL_0010: ret} // end of testmethod class ::. ctor

 

 

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.