在.NET/C#中,一个类中多半会有成员变量,成员变量可以不初始化,也可以初始化,如果不初始化,编译器会按不同的类型给它们一个默认值,引用类型的是NULL值。当然最好是把情况掌握在自己手中,初始化一下比较好。
如果自己初始化,对于普通的成员变量,就有两种情况,一种是在声明的时候就初始化,另一种是在构造函数中初始化。
如果是在声明时就初始化了,而在构造函数中没有初始化,情况很正常。如果既在声明时初始化,又在构造函数中初始化,那么按先后顺序,以构造函数中初始化的为准。但是有这样一个问题存在,如下的代码中:
1 using System;
2 namespace Test2
3 {
4 class ABC
5 {
6 public int i1=100;
7 public string s="abc";
8
9 public ABC()
10 {
11 }
12 public ABC(int i1):this()
13 {
14 this.i1 = i1;
15 }
16 public ABC(int i1, string s):this()
17 {
18 this.i1 = i1;
19 this.s = s;
20 }
21 }
22
23 class Program
24 {
25 static void Main(string[] args)
26 {
27 ABC abc = new ABC();
28 Console.WriteLine(abc.i1);
29 Console.WriteLine(abc.s);
30 Console.ReadLine();
31 }
32 }
33 }
我们看一下用ILDASM反汇编这个ASSEMBLY的结果:
将三个构造函数反汇编出来的IL代码仔细观察,
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.s 100
IL_0003: stfld int32 Test2.ABC::i1
IL_0008: ldarg.0
IL_0009: ldstr "abc"
IL_000e: stfld string Test2.ABC::s
IL_0013: ldarg.0
IL_0014: call instance void [mscorlib]System.Object::.ctor()
IL_0019: nop
IL_001a: nop
IL_001b: nop
IL_001c: ret
} // end of method ABC::.ctor
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.s 100
IL_0003: stfld int32 Test2.ABC::i1
IL_0008: ldarg.0
IL_0009: ldstr "abc"
IL_000e: stfld string Test2.ABC::s
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 Test2.ABC::i1
IL_0022: nop
IL_0023: ret
} // end of method ABC::.ctor
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.s 100
IL_0003: stfld int32 Test2.ABC::i1
IL_0008: ldarg.0
IL_0009: ldstr "abc"
IL_000e: stfld string Test2.ABC::s
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 Test2.ABC::i1
IL_0022: ldarg.0
IL_0023: ldarg.2
IL_0024: stfld string Test2.ABC::s
IL_0029: nop
IL_002a: ret
} // end of method ABC::.ctor
看到了没,在类中跟在成员变量声明后的这一块初始化语句:
IL_0001: ldc.i4.s 100
IL_0003: stfld int32 Test2.ABC::i1
IL_0008: ldarg.0
IL_0009: ldstr "abc"
IL_000e: stfld string Test2.ABC::s
被默认拷贝到了每一个构造函数中去。。。。。这儿只有2个成员变量,所以问题也不大,但如果有几十个成员变量,将会产生大量的冗余代码,最后生成的ASSEMBLY可能会显著变大,所以这不是一个好的做法。
好的做法是,把成员变量的初始化放在一个空参构造函数中,然后在非空参构造函数时多调用一次空参构造函数,修正如下:
1 using System;
2 namespace Test2
3 {
4 class ABC
5 {
6 public int i1;
7 public string s;
8
9 public ABC()
10 {
11 i1 = 100;
12 s = "abc";
13 }
14 public ABC(int i1):this()
15 {
16 this.i1 = i1;
17 }
18 public ABC(int i1, string s):this()
19 {
20 this.i1 = i1;
21 this.s = s;
22 }
23 }
24
25 class Program
26 {
27 static void Main(string[] args)
28 {
29 ABC abc = new ABC();
30 Console.WriteLine(abc.i1);
31 Console.WriteLine(abc.s);
32 Console.ReadLine();
33 }
34 }
35 }
36
这次我们再来看反汇编得到的IL代码:
就结构上来看跟前次是一模一样的,再具体展开三个构造函数:
1 .maxstack 8
2 IL_0000: ldarg.0
3 IL_0001: call instance void [mscorlib]System.Object::.ctor()
4 IL_0006: nop
5 IL_0007: nop
6 IL_0008: ldarg.0
7 IL_0009: ldc.i4.s 100
8 IL_000b: stfld int32 Test2.ABC::i1
9 IL_0010: ldarg.0
10 IL_0011: ldstr "abc"
11 IL_0016: stfld string Test2.ABC::s
12 IL_001b: nop
13 IL_001c: ret
14 } // end of method ABC::.ctor
15
1 .maxstack 8
2 IL_0000: ldarg.0
3 IL_0001: call instance void Test2.ABC::.ctor()
4 IL_0006: nop
5 IL_0007: nop
6 IL_0008: ldarg.0
7 IL_0009: ldarg.1
8 IL_000a: stfld int32 Test2.ABC::i1
9 IL_000f: nop
10 IL_0010: ret
11 } // end of method ABC::.ctor
12
1 .maxstack 8
2 IL_0000: ldarg.0
3 IL_0001: call instance void Test2.ABC::.ctor()
4 IL_0006: nop
5 IL_0007: nop
6 IL_0008: ldarg.0
7 IL_0009: ldarg.1
8 IL_000a: stfld int32 Test2.ABC::i1
9 IL_000f: ldarg.0
10 IL_0010: ldarg.2
11 IL_0011: stfld string Test2.ABC::s
12 IL_0016: nop
13 IL_0017: ret
14 } // end of method ABC::.ctor
诚然如我们所见,重复的初始化模块是没有了,但是这个call instance void Test2.ABC::.ctor() 在每一个其它构造函数中还是被重复了。
我们一般认为类成员变量要手动初始化比较好,而在成员变量刚一声明就初始化会导致多个构造函数中重复初始化,而改用空参构造函数再调用的方式又会引起构造函数被多次调用,那么后一种方法倒底有没有改善呢?我做了一个实验如下:(代码很长,打开需谨慎~!)
1 using System;
2 namespace Test2
3 {
4 class ABC
5 {
6 public string s1="this is a test!";
7 public string s2 = "this is a test!";
8 public string s3 = "this is a test!";
9 public string s4 = "this is a test!";
10 public string s5 = "this is a test!";
11 public string s6 = "this is a test!";
12 public string s7 = "this is a test!";
13 public string s8 = "this is a test!";
14 public string s9 = "this is a test!";
15 public string s10 = "this is a test!";
16 public string s11 = "this is a test!";
17 public string s12 = "this is a test!";
18 public string s13 = "this is a test!";
19 public string s14 = "this is a test!";
20 public string s15 = "this is a test!";
21 public string s16 = "this is a test!";
22 public string s17 = "this is a test!";
23 public string s18 = "this is a test!";
24 public string s19 = "this is a test!";
25 public string s20 = "this is a test!";
26 public string s21 = "this is a test!";
27 public string s22 = "this is a test!";
28 public string s23 = "this is a test!";
29 public string s24 = "this is a test!";
30 public string s25 = "this is a test!";
31 public string s26 = "this is a test!";
32 public string s27 = "this is a test!";
33 public string s28 = "this is a test!";
34 public string s29 = "this is a test!";
35 public string s30 = "this is a test!";
36 public string s31 = "this is a test!";
37 public string s32 = "this is a test!";
38 public string s33 = "this is a test!";
39 public string s34 = "this is a test!";
40 public string s35 = "this is a test!";
41 public string s36 = "this is a test!";
42 public string s37 = "this is a test!";
43 public string s38 = "this is a test!";
44 public string s39 = "this is a test!";
45 public string s40 = "this is a test!";
46 public string s41 = "this is a test!";
47 public string s42 = "this is a test!";
48 public string s43 = "this is a test!";
49 public string s44 = "this is a test!";
50 public string s45 = "this is a test!";
51 public string s46 = "this is a test!";
52 public string s47 = "this is a test!";
53 public string s48 = "this is a test!";
54 public string s49 = "this is a test!";
55 public string s50 = "this is a test!";
56 public string s51 = "this is a test!";
57 public string s52 = "this is a test!";
58 public string s53 = "this is a test!";
59 public string s54 = "this is a test!";
60 public string s55 = "this is a test!";
61 public string s56 = "this is a test!";
62 public string s57 = "this is a test!";
63 public string s58 = "this is a test!";
64 public string s59 = "this is a test!";
65 public string s60 = "this is a test!";
66 public string s61 = "this is a test!";
67 public string s62 = "this is a test!";
68 public string s63 = "this is a test!";
69 public string s64 = "this is a test!";
70 public string s65 = "this is a test!";
71 public string s66 = "this is a test!";
72 public string s67 = "this is a test!";
73 public string s68 = "this is a test!";
74 public string s69 = "this is a test!";
75 public string s70 = "this is a test!";
76 public string s71 = "this is a test!";
77 public string s72 = "this is a test!";
78 public string s73 = "this is a test!";
79 public string s74 = "this is a test!";
80 public string s75 = "this is a test!";
81 public string s76 = "this is a test!";
82 public string s77 = "this is a test!";
83 public string s78 = "this is a test!";
84 public string s79 = "this is a test!";
85 public string s80 = "this is a test!";
86 public string s81 = "this is a test!";
87 public string s82 = "this is a test!";
88 public string s83 = "this is a test!";
89 public string s84 = "this is a test!";
90 public string s85 = "this is a test!";
91 public string s86 = "this is a test!";
92 public string s87 = "this is a test!";
93 public string s88 = "this is a test!";
94 public string s89 = "this is a test!";
95 public string s90 = "this is a test!";
96 public string s91 = "this is a test!";
97 public string s92 = "this is a test!";
98 public string s93 = "this is a test!";
99 public string s94 = "this is a test!";
100 public string s95 = "this is a test!";
101 public string s96 = "this is a test!";
102 public string s97 = "this is a test!";
103 public string s98 = "this is a test!";
104 public string s99 = "this is a test!";
105 public string s100 = "this is a test!";
106 public string s101 = "this is a test!";
107 public string s102 = "this is a test!";
108 public string s103 = "this is a test!";
109 public string s104 = "this is a test!";
110 public string s105 = "this is a test!";
111 public string s106 = "this is a test!";
112 public string s107 = "this is a test!";
113 public string s108 = "this is a test!";
114 public string s109 = "this is a test!";
115 public string s110 = "this is a test!";
116 public string s111 = "this is a test!";
117 public string s112 = "this is a test!";
118 public string s113 = "this is a test!";
119 public string s114 = "this is a test!";
120 public string s115 = "this is a test!";
121 public string s116 = "this is a test!";
122 public string s117 = "this is a test!";
123 public string s118 = "this is a test!";
124 public string s119 = "this is a test!";
125 public string s120 = "this is a test!";
126 public string s121 = "this is a test!";
127 public string s122 = "this is a test!";
128 public string s123 = "this is a test!";
129 public string s124 = "this is a test!";
130 public string s125 = "this is a test!";
131 public string s126 = "this is a test!";
132 public string s127 = "this is a test!";
133 public string s128 = "this is a test!";
134 public string s129 = "this is a test!";
135 public string s130 = "this is a test!";
136 public string s131 = "this is a test!";
137 public string s132 = "this is a test!";
138 public string s133 = "this is a test!";
139 public string s134 = "this is a test!";
140 public string s135 = "this is a test!";
141 public string s136 = "this is a test!";
142 public string s137 = "this is a test!";
143 public string s138 = "this is a test!";
144 public string s139 = "this is a test!";
145 public string s140 = "this is a test!";
146 public string s141 = "this is a test!";
147 public string s142 = "this is a test!";
148 public string s143 = "this is a test!";
149 public string s144 = "this is a test!";
150 public string s145 = "this is a test!";
151 public string s146 = "this is a test!";
152 public string s147 = "this is a test!";
153 public string s148 = "this is a test!";
154 public string s149 = "this is a test!";
155 public string s150 = "this is a test!";
156 public string s151 = "this is a test!";
157 public string s152 = "this is a test!";
158 public string s153 = "this is a test!";
159 public string s154 = "this is a test!";
160 public string s155 = "this is a test!";
161 public string s156 = "this is a test!";
162 public string s157 = "this is a test!";
163 public string s158 = "this is a test!";
164 public string s159 = "this is a test!";
165 public string s160 = "this is a test!";
166 public string s161 = "this is a test!";
167 public string s162 = "this is a test!";
168 public string s163 = "this is a test!";
169 public string s164 = "this is a test!";
170 public string s165 = "this is a test!";
171 public string s166 = "this is a test!";
172 public string s167 = "this is a test!";
173 public string s168 = "this is a test!";
174 public string s169 = "this is a test!";
175 public string s170 = "this is a test!";
176 public string s171 = "this is a test!";
177 public string s172 = "this is a test!";
178 public string s173 = "this is a test!";
179 public string s174 = "this is a test!";
180 public string s175 = "this is a test!";
181 public string s176 = "this is a test!";
182 public string s177 = "this is a test!";
183 public string s178 = "this is a test!";
184 public string s179 = "this is a test!";
185 public string s180 = "this is a test!";
186 public string s181 = "this is a test!";
187 public string s182 = "this is a test!";
188 public string s183 = "this is a test!";
189 public string s184 = "this is a test!";
190 public string s185 = "this is a test!";
191 public string s186 = "this is a test!";
192 public string s187 = "this is a test!";
193 public string s188 = "this is a test!";
194 public string s189 = "this is a test!";
195 public string s190 = "this is a test!";
196 public string s191 = "this is a test!";
197 public string s192 = "this is a test!";
198 public string s193 = "this is a test!";
199 public string s194 = "this is a test!";
200 public string s195 = "this is a test!";
201 public string s196 = "this is a test!";
202 public string s197 = "this is a test!";
203 public string s198 = "this is a test!";
204 public string s199 = "this is a test!";
205 public string s200 = "this is a test!";
206
207
208 public ABC()
209 {
210 }
211 public ABC(string s1)
212 {
213 this.s1 = s1;
214 }
215 public ABC(string s1, string s2)
216 {
217 this.s1 = s1;
218 this.s2 = s2;
219 }
220 public ABC(string s1, string s2,string s3)
221 {
222 this.s1 = s1;
223 this.s2 = s2;
224 this.s3 = s3;
225 }
226 public ABC(string s1, string s2,string s3,string s4)
227 {
228 this.s1 = s1;
229 this.s2 = s2;
230 this.s3 = s3;
231 this.s4 = s4;
232 }
233 public ABC(string s1, string s2,string s3,string s4,string s5)
234 {
235 this.s1 = s1;
236 this.s2 = s2;
237 this.s3 = s3;
238 this.s4 = s4;
239 this.s5 = s5;
240 }
241 }
242
243 class Program
244 {
245 static void Main(string[] args)
246 {
247 ABC abc = new ABC();
248 Console.WriteLine(abc.s1);
249 Console.ReadLine();
250 }
251 }
252 }
253
在其中这个类有200个string型的成员变量,全部使用在声明后面直接初始化的方法,得到的ASSEMBLY 大小是 20,480 字节。
然后我用后面一种不在声明后直接初始化,而在无参类中初始化,然后在各个构造函数中调用无参构造函数的方法重新实现这个程序:
(代码很长,打开需谨慎~!)
1 using System;
2 namespace Test2
3 {
4 class ABC
5 {
6 public string s1;
7 public string s2;
8 public string s3;
9 public string s4;
10 public string s5;
11 public string s6;
12 public string s7;
13 public string s8;
14 public string s9;
15 public string s10;
16 public string s11;
17 public string s12;
18 public string s13;
19 public string s14;
20 public string s15;
21 public string s16;
22 public string s17;
23 public string s18;
24 public string s19;
25 public string s20;
26 public string s21;
27 public string s22;
28 public string s23;
29 public string s24;
30 public string s25;
31 public string s26;
32 public string s27;
33 public string s28;
34 public string s29;
35 public string s30;
36 public string s31;
37 public string s32;
38 public string s33;
39 public string s34;
40 public string s35;
41 public string s36;
42 public string s37;
43 public string s38;
44 public string s39;
45 public string s40;
46 public string s41;
47 public string s42;
48 public string s43;
49 public string s44;
50 public string s45;
51 public string s46;
52 public string s47;
53 public string s48;
54 public string s49;
55 public string s50;
56 public string s51;
57 public string s52;
58 public string s53;
59 public string s54;
60 public string s55;
61 public string s56;
62 public string s57;
63 public string s58;
64 public string s59;
65 public string s60;
66 public string s61;
67 public string s62;
68 public string s63;
69 public string s64;
70 public string s65;
71 public string s66;
72 public string s67;
73 public string s68;
74 public string s69;
75 public string s70;
76 public string s71;
77 public string s72;
78 public string s73;
79 public string s74;
80 public string s75;
81 public string s76;
82 public string s77;
83 public string s78;
84 public string s79;
85 public string s80;
86 public string s81;
87 public string s82;
88 public string s83;
89 public string s84;
90 public string s85;
91 public string s86;
92 public string s87;
93 public string s88;
94 public string s89;
95 public string s90;
96 public string s91;
97 public string s92;
98 public string s93;
99 public string s94;
100 public string s95;
101 public string s96;
102 public string s97;
103 public string s98;
104 public string s99;
105 public string s100;
106 public string s101;
107 public string s102;
108 public string s103;
109 public string s104;
110 public string s105;
111 public string s106;
112 public string s107;
113 public string s108;
114 public string s109;
115 public string s110;
116 public string s111;
117 public string s112;
118 public string s113;
119 public string s114;
120 public string s115;
121 public string s116;
122 public string s117;
123 public string s118;
124 public string s119;
125 public string s120;
126 public string s121;
127 public string s122;
128 public string s123;
129 public string s124;
130 public string s125;
131 public string s126;
132 public string s127;
133 public string s128;
134 public string s129;
135 public string s130;
136 public string s131;
137 public string s132;
138 public string s133;
139 public string s134;
140 public string s135;
141 public string s136;
142 public string s137;
143 public string s138;
144 public string s139;
145 public string s140;
146 public string s141;
147 public string s142;
148 public string s143;
149 public string s144;
150 public string s145;
151 public string s146;
152 public string s147;
153 public string s148;
154 public string s149;
155 public string s150;
156 public string s151;
157 public string s152;
158 public string s153;
159 public string s154;
160 public string s155;
161 public string s156;
162 public string s157;
163 public string s158;
164 public string s159;
165 public string s160;
166 public string s161;
167 public string s162;
168 public string s163;
169 public string s164;
170 public string s165;
171 public string s166;
172 public string s167;
173 public string s168;
174 public string s169;
175 public string s170;
176 public string s171;
177 public string s172;
178 public string s173;
179 public string s174;
180 public string s175;
181 public string s176;
182 public string s177;
183 public string s178;
184 public string s179;
185 public string s180;
186 public string s181;
187 public string s182;
188 public string s183;
189 public string s184;
190 public string s185;
191 public string s186;
192 public string s187;
193 public string s188;
194 public string s189;
195 public string s190;
196 public string s191;
197 public string s192;
198 public string s193;
199 public string s194;
200 public string s195;
201 public string s196;
202 public string s197;
203 public string s198;
204 public string s199;
205 public string s200;
206
207
208
209 public ABC()
210 {
211 s1 = "this is a test!";
212 s2 = "this is a test!";
213 s3 = "this is a test!";
214 s4 = "this is a test!";
215 s5 = "this is a test!";
216 s6 = "this is a test!";
217 s7 = "this is a test!";
218 s8 = "this is a test!";
219 s9 = "this is a test!";
220 s10 = "this is a test!";
221 s11 = "this is a test!";
222 s12 = "this is a test!";
223 s13 = "this is a test!";
224 s14 = "this is a test!";
225 s15 = "this is a test!";
226 s16 = "this is a test!";
227 s17 = "this is a test!";
228 s18 = "this is a test!";
229 s19 = "this is a test!";
230 s20 = "this is a test!";
231 s21 = "this is a test!";
232 s22 = "this is a test!";
233 s23 = "this is a test!";
234 s24 = "this is a test!";
235 s25 = "this is a test!";
236 s26 = "this is a test!";
237 s27 = "this is a test!";
238 s28 = "this is a test!";
239 s29 = "this is a test!";
240 s30 = "this is a test!";
241 s31 = "this is a test!";
242 s32 = "this is a test!";
243 s33 = "this is a test!";
244 s34 = "this is a test!";
245 s35 = "this is a test!";
246 s36 = "this is a test!";
247 s37 = "this is a test!";
248 s38 = "this is a test!";
249 s39 = "this is a test!";
250 s40 = "this is a test!";
251 s41 = "this is a test!";
252 s42 = "this is a test!";
253 s43 = "this is a test!";
254 s44 = "this is a test!";
255 s45 = "this is a test!";
256 s46 = "this is a test!";
257 s47 = "this is a test!";
258 s48 = "this is a test!";
259 s49 = "this is a test!";
260 s50 = "this is a test!";
261 s51 = "this is a test!";
262 s52 = "this is a test!";
263 s53 = "this is a test!";
264 s54 = "this is a test!";
265 s55 = "this is a test!";
266 s56 = "this is a test!";
267 s57 = "this is a test!";
268 s58 = "this is a test!";
269 s59 = "this is a test!";
270 s60 = "this is a test!";
271 s61 = "this is a test!";
272 s62 = "this is a test!";
273 s63 = "this is a test!";
274 s64 = "this is a test!";
275 s65 = "this is a test!";
276 s66 = "this is a test!";
277 s67 = "this is a test!";
278 s68 = "this is a test!";
279 s69 = "this is a test!";
280 s70 = "this is a test!";
281 s71 = "this is a test!";
282 s72 = "this is a test!";
283 s73 = "this is a test!";
284 s74 = "this is a test!";
285 s75 = "this is a test!";
286 s76 = "this is a test!";
287 s77 = "this is a test!";
288 s78 = "this is a test!";
289 s79 = "this is a test!";
290 s80 = "this is a test!";
291 s81 = "this is a test!";
292 s82 = "this is a test!";
293 s83 = "this is a test!";
294 s84 = "this is a test!";
295 s85 = "this is a test!";
296 s86 = "this is a test!";
297 s87 = "this is a test!";
298 s88 = "this is a test!";
299 s89 = "this is a test!";
300 s90 = "this is a test!";
301 s91 = "this is a test!";
302 s92 = "this is a test!";
303 s93 = "this is a test!";
304 s94 = "this is a test!";
305 s95 = "this is a test!";
306 s96 = "this is a test!";
307 s97 = "this is a test!";
308 s98 = "this is a test!";
309 s99 = "this is a test!";
310 s100 = "this is a test!";
311 s101 = "this is a test!";
312 s102 = "this is a test!";
313 s103 = "this is a test!";
314 s104 = "this is a test!";
315 s105 = "this is a test!";
316 s106 = "this is a test!";
317 s107 = "this is a test!";
318 s108 = "this is a test!";
319 s109 = "this is a test!";
320 s110 = "this is a test!";
321 s111 = "this is a test!";
322 s112 = "this is a test!";
323 s113 = "this is a test!";
324 s114 = "this is a test!";
325 s115 = "this is a test!";
326 s116 = "this is a test!";
327 s117 = "this is a test!";
328 s118 = "this is a test!";
329 s119 = "this is a test!";
330 s120 = "this is a test!";
331 s121 = "this is a test!";
332 s122 = "this is a test!";
333 s123 = "this is a test!";
334 s124 = "this is a test!";
335 s125 = "this is a test!";
336 s126 = "this is a test!";
337 s127 = "this is a test!";
338 s128 = "this is a test!";
339 s129 = "this is a test!";
340 s130 = "this is a test!";
341 s131 = "this is a test!";
342 s132 = "this is a test!";
343 s133 = "this is a test!";
344 s134 = "this is a test!";
345 s135 = "this is a test!";
346 s136 = "this is a test!";
347 s137 = "this is a test!";
348 s138 = "this is a test!";
349 s139 = "this is a test!";
350 s140 = "this is a test!";
351 s141 = "this is a test!";
352 s142 = "this is a test!";
353 s143 = "this is a test!";
354 s144 = "this is a test!";
355 s145 = "this is a test!";
356 s146 = "this is a test!";
357 s147 = "this is a test!";
358 s148 = "this is a test!";
359 s149 = "this is a test!";
360 s150 = "this is a test!";
361 s151 = "this is a test!";
362 s152 = "this is a test!";
363 s153 = "this is a test!";
364 s154 = "this is a test!";
365 s155 = "this is a test!";
366 s156 = "this is a test!";
367 s157 = "this is a test!";
368 s158 = "this is a test!";
369 s159 = "this is a test!";
370 s160 = "this is a test!";
371 s161 = "this is a test!";
372 s162 = "this is a test!";
373 s163 = "this is a test!";
374 s164 = "this is a test!";
375 s165 = "this is a test!";
376 s166 = "this is a test!";
377 s167 = "this is a test!";
378 s168 = "this is a test!";
379 s169 = "this is a test!";
380 s170 = "this is a test!";
381 s171 = "this is a test!";
382 s172 = "this is a test!";
383 s173 = "this is a test!";
384 s174 = "this is a test!";
385 s175 = "this is a test!";
386 s176 = "this is a test!";
387 s177 = "this is a test!";
388 s178 = "this is a test!";
389 s179 = "this is a test!";
390 s180 = "this is a test!";
391 s181 = "this is a test!";
392 s182 = "this is a test!";
393 s183 = "this is a test!";
394 s184 = "this is a test!";
395 s185 = "this is a test!";
396 s186 = "this is a test!";
397 s187 = "this is a test!";
398 s188 = "this is a test!";
399 s189 = "this is a test!";
400 s190 = "this is a test!";
401 s191 = "this is a test!";
402 s192 = "this is a test!";
403 s193 = "this is a test!";
404 s194 = "this is a test!";
405 s195 = "this is a test!";
406 s196 = "this is a test!";
407 s197 = "this is a test!";
408 s198 = "this is a test!";
409 s199 = "this is a test!";
410 s200 = "this is a test!";
411 }
412 public ABC(string s1)
413 {
414 this.s1 = s1;
415 }
416 public ABC(string s1, string s2)
417 {
418 this.s1 = s1;
419 this.s2 = s2;
420 }
421 public ABC(string s1, string s2,string s3)
422 {
423 this.s1 = s1;
424 this.s2 = s2;
425 this.s3 = s3;
426 }
427 public ABC(string s1, string s2,string s3,string s4)
428 {
429 this.s1 = s1;
430 this.s2 = s2;
431 this.s3 = s3;
432 this.s4 = s4;
433 }
434 public ABC(string s1, string s2,string s3,string s4,string s5)
435 {
436 this.s1 = s1;
437 this.s2 = s2;
438 this.s3 = s3;
439 this.s4 = s4;
440 this.s5 = s5;
441 }
442 }
443
444 class Program
445 {
446 static void Main(string[] args)
447 {
448 ABC abc = new ABC();
449 Console.WriteLine(abc.s1);
450 Console.ReadLine();
451 }
452 }
453 }
454
果然不一样,这次编译得到的ASSEMBLY大小是 9,216 字节,小了整整一个数量级,但是我们要注意,小是小了,但是重复在构造函数中调用函数,可能换来了执行时间上的开销,但是好在.NET程序使用的是JIT方式运行,所以只有方法使用到时才会调用它,应该问题也不大,所以综合来看,使用在无参构造函数中初始化成员变量的方法比较好!