这个初始化方法之间的区别是什么? [英] What is the difference between this initialization methods?
本文介绍了这个初始化方法之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
这两codeS之间的区别是什么?
Hi all
What is the difference between this two codes?
class SomeClass
{
SomeType val = new SomeType();
}
和
class SomeClass
{
SomeType val;
SomeClass()
{
val = new SomeType();
}
}
哪种方法preFERD?
Which method is preferd?
推荐答案
目前的几乎的他们之间没有区别。场的分配将在这两种情况下在构造内发生。有一个在如何happpens相对于基类的构造函数,虽然有差别。看看下面的code:
There is almost not difference between them. The assignment of the field will happen within the constructor in both cases. There is a difference in how this happpens in relation to base class constructors though. Take the following code:
class Base
{
public Base()
{
}
}
class One : Base
{
string test = "text";
}
class Two : Base
{
string test;
public Two()
{
test = "text";
}
}
在这种情况下,基类的构造函数将被调用的在的领域分配中的类一
,而在分配类两个
。
In this case the base class constructor will be invoked after the field assignment in the class One
, but before the assignment in class Two
.
这篇关于这个初始化方法之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文