(1)通过IL来看构造函数

一:实例构造函数
===================================
1
class TestConstruct{
        private int    var_int;
        private double var_double;
        private string var_string;
}
.ctor:void() //默认的无参构造函数

.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
  // 代码大小       7 (0x7)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  call       instance void [mscorlib]System.Object::.ctor()  //调用基类构造函数
  IL_0006:  ret
} // end of method TestConstruct::.ctor

===========================================


class TestConstruct{
        private int    var_int=2;
        private double var_double=2.5;
        private string var_string;
    }
.ctor:void() //默认的无参构造函数

.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
  // 代码大小       30 (0x1e)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.2
  IL_0002:  stfld      int32 ConsoleTest.TestConstruct::var_int
  IL_0007:  ldarg.0
  IL_0008:  ldc.r8     2.5                                                                     //现初始化默认值
  IL_0011:  stfld      float64 ConsoleTest.TestConstruct::var_double
  IL_0016:  ldarg.0
  IL_0017:  call       instance void [mscorlib]System.Object::.ctor()      //然后再调用基本构造函数
  IL_001c:  nop
  IL_001d:  ret
} // end of method TestConstruct::.ctor

比较 1 2 说明了什么问题。

=====================================================

3
class TestConstruct{
        private int    var_int=2;
        private double var_double=2.5;
        private string var_string;

        public TestConstruct(){

        }
    }
.ctor:void() //自定义的无参构造函数

.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
  // 代码大小       32 (0x20)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.2
  IL_0002:  stfld      int32 ConsoleTest.TestConstruct::var_int
  IL_0007:  ldarg.0
  IL_0008:  ldc.r8     2.5
  IL_0011:  stfld      float64 ConsoleTest.TestConstruct::var_double
  IL_0016:  ldarg.0
  IL_0017:  call       instance void [mscorlib]System.Object::.ctor()

  IL_001c:  nop
  IL_001d:  nop
  IL_001e:  nop
  IL_001f:  ret
} // end of method TestConstruct::.ctor

==========================================================


class TestConstruct{
        private int    var_int=2;
        private double var_double=2.5;
        private string var_string;

        public TestConstruct(){
            this.var_double = 9.8;
            this.var_string = "TestConstruct";
        }
    }

.ctor:void() //自定义的无参构造函数
.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
  // 代码大小       58 (0x3a)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.2
  IL_0002:  stfld      int32 ConsoleTest.TestConstruct::var_int
  IL_0007:  ldarg.0
  IL_0008:  ldc.r8     2.5                                                                         //执行默认无参构造函数
  IL_0011:  stfld      float64 ConsoleTest.TestConstruct::var_double
  IL_0016:  ldarg.0
  IL_0017:  call       instance void [mscorlib]System.Object::.ctor()          //然后执行基类构造函数

  IL_001c:  nop
  IL_001d:  nop
  IL_001e:  ldarg.0
  IL_001f:  ldc.r8     9.8000000000000007
  IL_0028:  stfld      float64 ConsoleTest.TestConstruct::var_double
  IL_002d:  ldarg.0
  IL_002e:  ldstr      "TestConstruct"               //最后才执行自定义无参默认构造函数中的代码
  IL_0033:  stfld      string ConsoleTest.TestConstruct::var_string
  IL_0038:  nop
  IL_0039:  ret
} // end of method TestConstruct::.ctor

比较 2 3 4说明一个问题:自定义无参构造函数的代码执行过程,具体见我在上面的注释
  这样的代码,变量的值改变2次,代码量也比较多,建议 使用构造函数中调用构造函数
==================================================================


class TestConstruct{
        private int    var_int=2;
        private double var_double=2.5;
        private string var_string;

        public TestConstruct() {
        }
        public TestConstruct(string var){
            this.var_string = var;
        }
    }

.ctor:void() //自定义的无参构造函数
.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
  // 代码大小       32 (0x20)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.2
  IL_0002:  stfld      int32 ConsoleTest.TestConstruct::var_int
  IL_0007:  ldarg.0
  IL_0008:  ldc.r8     2.5
  IL_0011:  stfld      float64 ConsoleTest.TestConstruct::var_double
  IL_0016:  ldarg.0
  IL_0017:  call       instance void [mscorlib]System.Object::.ctor()
  IL_001c:  nop
  IL_001d:  nop
  IL_001e:  nop
  IL_001f:  ret
} // end of method TestConstruct::.ctor


.ctor:void(string) //自定义的有参数构造函数
.method public hidebysig specialname rtspecialname
        instance void  .ctor(string var) cil managed
{
  // 代码大小       39 (0x27)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.2
  IL_0002:  stfld      int32 ConsoleTest.TestConstruct::var_int
  IL_0007:  ldarg.0
  IL_0008:  ldc.r8     2.5
  IL_0011:  stfld      float64 ConsoleTest.TestConstruct::var_double
  IL_0016:  ldarg.0
  IL_0017:  call       instance void [mscorlib]System.Object::.ctor()
  IL_001c:  nop
  IL_001d:  nop
  IL_001e:  ldarg.0
  IL_001f:  ldarg.1
  IL_0020:  stfld      string ConsoleTest.TestConstruct::var_string
  IL_0025:  nop
  IL_0026:  ret
} // end of method TestConstruct::.ctor

=====================================================================


 class TestConstruct{
        private int    var_int=2;
        private double var_double=2.5;
        private string var_string;

        public TestConstruct() {
        }
        public TestConstruct(string var){
            this.var_double=9.5;
            this.var_string = var;
        }
    }

.ctor:void() //自定义的无参构造函数
.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
  // 代码大小       32 (0x20)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.2
  IL_0002:  stfld      int32 ConsoleTest.TestConstruct::var_int
  IL_0007:  ldarg.0
  IL_0008:  ldc.r8     2.5
  IL_0011:  stfld      float64 ConsoleTest.TestConstruct::var_double
  IL_0016:  ldarg.0
  IL_0017:  call       instance void [mscorlib]System.Object::.ctor()
  IL_001c:  nop
  IL_001d:  nop
  IL_001e:  nop
  IL_001f:  ret
} // end of method TestConstruct::.ctor


.method public hidebysig specialname rtspecialname
        instance void  .ctor(string var) cil managed
{
  // 代码大小       54 (0x36)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.2
  IL_0002:  stfld      int32 ConsoleTest.TestConstruct::var_int
  IL_0007:  ldarg.0
  IL_0008:  ldc.r8     2.5
  IL_0011:  stfld      float64 ConsoleTest.TestConstruct::var_double
  IL_0016:  ldarg.0
  IL_0017:  call       instance void [mscorlib]System.Object::.ctor()
  IL_001c:  nop
  IL_001d:  nop
  IL_001e:  ldarg.0
  IL_001f:  ldc.r8     9.5
  IL_0028:  stfld      float64 ConsoleTest.TestConstruct::var_double
  IL_002d:  ldarg.0
  IL_002e:  ldarg.1
  IL_002f:  stfld      string ConsoleTest.TestConstruct::var_string
  IL_0034:  nop
  IL_0035:  ret
} // end of method TestConstruct::.ctor

=====================================================================

二:继承关系中的构造函数

class Father
    {
        private int father_int;
        private Double father_double;

        public Father()
        {
            this.father_int = 1;
            this.father_double = 1.1;
        }
    }
    class Son : Father
    {
        private int son_int;
        private double son_double;

        public Son()
        {
            this.son_int = 2;
            this.son_double = 2.2;
        }
       
    }

//====== .ctor() Son的自定义构造函数============
.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
  // 代码大小       32 (0x20)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  call       instance void ConsoleTest.Father::.ctor()  //先调用基类的构造函数
  IL_0006:  nop
  IL_0007:  nop
  IL_0008:  ldarg.0
  IL_0009:  ldc.i4.2
  IL_000a:  stfld      int32 ConsoleTest.Son::son_int   //然后再执行子类中的代码
  IL_000f:  ldarg.0
  IL_0010:  ldc.r8     2.2000000000000002
  IL_0019:  stfld      float64 ConsoleTest.Son::son_double
  IL_001e:  nop
  IL_001f:  ret
} // end of method Son::.ctor

//====== .ctor() Father的自定义构造函数============
.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
  // 代码大小       32 (0x20)
  .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:  ldc.i4.1
  IL_000a:  stfld      int32 ConsoleTest.Father::father_int
  IL_000f:  ldarg.0
  IL_0010:  ldc.r8     1.1000000000000001
  IL_0019:  stfld      float64 ConsoleTest.Father::father_double
  IL_001e:  nop
  IL_001f:  ret
} // end of method Father::.ctor

=============================================================

含有Base

    class Father
    {
        private int father_int;
        private Double father_double;

        public Father(int x)
        {
            this.father_int = x;
            this.father_double = 1.1;
        }
    }
    class Son : Father
    {
        private int son_int;
        private double son_double;


        public Son(int father_x):base(father_x)
        {
            this.son_int = 2;
            this.son_double = 2.2;
        }
       
    }

//====== .ctor(int32) Father的自定义构造函数============
.method public hidebysig specialname rtspecialname
        instance void  .ctor(int32 x) cil managed
{
  // 代码大小       32 (0x20)
  .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:  ldarg.1
  IL_000a:  stfld      int32 ConsoleTest.Father::father_int
  IL_000f:  ldarg.0
  IL_0010:  ldc.r8     1.1000000000000001
  IL_0019:  stfld      float64 ConsoleTest.Father::father_double
  IL_001e:  nop
  IL_001f:  ret
} // end of method Father::.ctor


//====== .ctor(int 32) Son的自定义构造函数============
.method public hidebysig specialname rtspecialname
        instance void  .ctor(int32 father_x) cil managed
{
  // 代码大小       33 (0x21)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldarg.1
  IL_0002:  call       instance void ConsoleTest.Father::.ctor(int32)
  IL_0007:  nop
  IL_0008:  nop
  IL_0009:  ldarg.0
  IL_000a:  ldc.i4.2
  IL_000b:  stfld      int32 ConsoleTest.Son::son_int
  IL_0010:  ldarg.0
  IL_0011:  ldc.r8     2.2000000000000002
  IL_001a:  stfld      float64 ConsoleTest.Son::son_double
  IL_001f:  nop
  IL_0020:  ret
} // end of method Son::.ctor

=====================================================================
class Father
    {
        private int father_int;
        private Double father_double;

        public Father(int x)
        {
            this.father_int = x;
            this.father_double = 1.1;
        }
    }
    class Son : Father
    {
        private int son_int;
        private double son_double;

        public Son()
        {
            this.son_int = 2;
            this.son_double = 2.2;
 
        }
  
    }
编译时候会报错误 Father是有参数构构造函数
=====================================================


三:构造函数调用构造函数
 class Son
    {
        private int son_int=3;
        private double son_double;

        public Son()
        {
            this.son_int = 2;
            //this.son_double = 2.2;
 
        }
        public Son(double son_double):this()
        {
            this.son_double =son_double;          
        }
       
    }


=====.ctor()============================
.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
  // 代码大小       24 (0x18)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldc.i4.3
  IL_0002:  stfld      int32 ConsoleTest.Son::son_int
  IL_0007:  ldarg.0
  IL_0008:  call       instance void [mscorlib]System.Object::.ctor()
  IL_000d:  nop
  IL_000e:  nop
  IL_000f:  ldarg.0
  IL_0010:  ldc.i4.2
  IL_0011:  stfld      int32 ConsoleTest.Son::son_int
  IL_0016:  nop
  IL_0017:  ret
} // end of method Son::.ctor

=======.ctor(float64 )================
.method public hidebysig specialname rtspecialname
        instance void  .ctor(float64 son_double) cil managed
{
  // 代码大小       17 (0x11)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  call       instance void ConsoleTest.Son::.ctor()
  IL_0006:  nop
  IL_0007:  nop
  IL_0008:  ldarg.0
  IL_0009:  ldarg.1
  IL_000a:  stfld      float64 ConsoleTest.Son::son_double
  IL_000f:  nop
  IL_0010:  ret
} // end of method Son::.ctor

=====================================================


四:结构体的构造函数
struct TestStruct
    {
        int struct_int;
        double struct_double;
    }


=============================================
//错误:结构中不能有实例字段初始值设定项
struct TestStruct
    {
        int struct_int=3;
        double struct_double;
    }
==============================================
//错误 :结构不能包含显式的无参数构造函数
 struct TestStruct
    {
        int struct_int;
        double struct_double;

        public TestStruct()
        {
        }
    }
==============================================
//错误1 在控制离开构造函数之前,字段“ConsoleTest.TestStruct.struct_int”必须完全赋值 
  错误2  在控制离开构造函数之前,字段“ConsoleTest.TestStruct.struct_double”必须完全赋值
 struct TestStruct
    {
        int struct_int;
        double struct_double;

        public TestStruct(int x,double y)
        {
           
        }
    }
==============================================
OK
struct TestStruct
    {
        int struct_int;
        double struct_double;

        public TestStruct(int x,double y)
        {
            struct_double = y;
            struct_int = x;
 
        }
    }
ok
struct TestStruct
    {
        int struct_int;
        double struct_double;

        public TestStruct(int x,double y)
        {
            struct_double = 3;
            struct_int = x;
 
        }
    }

==.ctor(int32,float64)============
.method public hidebysig specialname rtspecialname
        instance void  .ctor(int32 x,
                             float64 y) cil managed
{
  // 代码大小       24 (0x18)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldc.r8     3.
  IL_000b:  stfld      float64 ConsoleTest.TestStruct::struct_double
  IL_0010:  ldarg.0
  IL_0011:  ldarg.1
  IL_0012:  stfld      int32 ConsoleTest.TestStruct::struct_int
  IL_0017:  ret
} // end of method TestStruct::.ctor

原文地址:https://www.cnblogs.com/lzh/p/739881.html