内联初始化字段与类实例构造器

首先解释一下内联初始化的内联的意思:就是在声明字段的时候同时进行初始化赋值。

举个简单的例子就是:
class SomeType()
{
      int x = 5;
}
这里为x赋值为5实际上发生在SomeType的构造器中,IL代码可以作证:
SomeType的构造函数的IL代码如下:

None.gif .method  public  hidebysig specialname rtspecialname 
None.gif        instance 
void   .ctor() cil managed
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
// 代码大小       14 (0xe)
InBlock.gif
  .maxstack  2
InBlock.gif  IL_0000:  ldarg.
0
InBlock.gif  IL_0001:  ldc.i4.
5
InBlock.gif  IL_0002:  stfld      int32 SomeType::x
InBlock.gif  IL_0007:  ldarg.
0
InBlock.gif  IL_0008:  call       instance 
void [mscorlib]System.Object::.ctor()
InBlock.gif  IL_000d:  ret
ExpandedBlockEnd.gif}
  //  end of method SomeType::.ctor
None.gif

None.gif

我们可以看到确实如此。
所以说内联初始化字段只是C#提供的一种简化语法,但是往往会造成代码的膨胀。

比如说:

None.gif class  SomeType
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int x = 5;
InBlock.gif    
string s = "Hi there";
InBlock.gif    
double d = 3.14159;
InBlock.gif    
byte b;
InBlock.gif    
InBlock.gif    
public SomeType()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{}
InBlock.gif    
public SomeType(int x)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public SomeType(string s)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

编译以后查看IL代码:
其中有三个构造函数:
.ctor : void(int32)的代码为:
None.gif .method  public  hidebysig specialname rtspecialname 
None.gif        instance 
void   .ctor(int32 x) cil managed
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
// 代码大小       40 (0x28)
InBlock.gif
  .maxstack  2
InBlock.gif  IL_0000:  ldarg.
0
InBlock.gif  IL_0001:  ldc.i4.
5
InBlock.gif  IL_0002:  stfld      int32 SomeType::x
InBlock.gif  IL_0007:  ldarg.
0
InBlock.gif  IL_0008:  ldstr      
"Hi there"
InBlock.gif  IL_000d:  stfld      
string SomeType::s
InBlock.gif  IL_0012:  ldarg.
0
InBlock.gif  IL_0013:  ldc.r8     
3.1415899999999999
InBlock.gif  IL_001c:  stfld      float64 SomeType::d
InBlock.gif  IL_0021:  ldarg.
0
InBlock.gif  IL_0022:  call       instance 
void [mscorlib]System.Object::.ctor()
InBlock.gif  IL_0027:  ret
ExpandedBlockEnd.gif}
  //  end of method SomeType::.ctor
None.gif

.ctor : void(string)的IL代码:

None.gif .method  public  hidebysig specialname rtspecialname 
None.gif        instance 
void   .ctor( string  s) cil managed
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
// 代码大小       40 (0x28)
InBlock.gif
  .maxstack  2
InBlock.gif  IL_0000:  ldarg.
0
InBlock.gif  IL_0001:  ldc.i4.
5
InBlock.gif  IL_0002:  stfld      int32 SomeType::x
InBlock.gif  IL_0007:  ldarg.
0
InBlock.gif  IL_0008:  ldstr      
"Hi there"
InBlock.gif  IL_000d:  stfld      
string SomeType::s
InBlock.gif  IL_0012:  ldarg.
0
InBlock.gif  IL_0013:  ldc.r8     
3.1415899999999999
InBlock.gif  IL_001c:  stfld      float64 SomeType::d
InBlock.gif  IL_0021:  ldarg.
0
InBlock.gif  IL_0022:  call       instance 
void [mscorlib]System.Object::.ctor()
InBlock.gif  IL_0027:  ret
ExpandedBlockEnd.gif}
  //  end of method SomeType::.ctor

.ctor : void()的IL代码为:

None.gif .method  public  hidebysig specialname rtspecialname 
None.gif        instance 
void   .ctor() cil managed
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
// 代码大小       40 (0x28)
InBlock.gif
  .maxstack  2
InBlock.gif  IL_0000:  ldarg.
0
InBlock.gif  IL_0001:  ldc.i4.
5
InBlock.gif  IL_0002:  stfld      int32 SomeType::x
InBlock.gif  IL_0007:  ldarg.
0
InBlock.gif  IL_0008:  ldstr      
"Hi there"
InBlock.gif  IL_000d:  stfld      
string SomeType::s
InBlock.gif  IL_0012:  ldarg.
0
InBlock.gif  IL_0013:  ldc.r8     
3.1415899999999999
InBlock.gif  IL_001c:  stfld      float64 SomeType::d
InBlock.gif  IL_0021:  ldarg.
0
InBlock.gif  IL_0022:  call       instance 
void [mscorlib]System.Object::.ctor()
InBlock.gif  IL_0027:  ret
ExpandedBlockEnd.gif}
  //  end of method SomeType::.ctor

这回清楚了吧,三个构造函数初始化了三次。所以最好避免在声明的时候初始化字段,而放在构造其中去进行,这样有助于减小代码尺寸,所以我们改写如下(假设每个构造器都需要初始化这些字段):

None.gif class  SomeType
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int x;
InBlock.gif    
string s;
InBlock.gif    
double d;
InBlock.gif    
byte b;
InBlock.gif    
InBlock.gif    
public SomeType()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        x 
= 5;
InBlock.gif        s 
= "Hi there";
InBlock.gif        d 
= 3.14159;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public SomeType(int x) : this()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.x = x;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public SomeType(string s) : this()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.s = s;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

我们把公共的初始化代码放到默认构造器中,其他的构造器都先调用默认构造器就达到了我们的目的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值