问题描述
我正在为我的对象进行对象初始化和构造函数初始化,但是无法准确回答我的问题.这里的Case1和Case2有什么区别?
I was going through object initialization and constructor initialization for my object, but couldn't get exact reply to my question.What is the difference between Case1 and Case2 here;
案例1:
namespace ConsoleApplication2
{
class MyBuilder
{
private MySynchronizer m_synchronizer = new MySynchronizer();
public MyBuilder()
{
}
public void ProcessRecord(int recordNumber)
{
m_synchronizer.Process(recordNumber);
}
}
}
第二种情况:
namespace ConsoleApplication2
{
class MyBuilder
{
private MySynchronizer m_synchronizer;
public MyBuilder()
{
m_synchronizer = new MySynchronizer();
}
public void ProcessRecord(int recordNumber)
{
m_synchronizer.Process(recordNumber);
}
}
}
这是示例代码,显示了我如何调用Builder类;
This is sample code to show how I am calling my Builder class;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press any key to stop");
MyBuilder builder = new MyBuilder();
builder.ProcessRecord(2);
}
}
[对不起,如果我不能正确地改写问题,在这种情况下,任何人都可以提供指向其他SO文章的链接]
[Sorry, if I could not have rephrase the question properly, in which case anyone can provide the link to other SO article]
推荐答案
这里的区别是确实细微,只能在IL中轻松理解:
The difference here is really subtle, and can only easily be appreciated in IL:
class MyBuilder1
{
private MySynchronizer m_synchronizer = new MySynchronizer();
public MyBuilder1()
{
}
}
为我们提供了构造函数:
gives us the constructor:
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2050
// Code size 18 (0x12)
.maxstack 8
IL_0000: ldarg.0
IL_0001: newobj instance void MySynchronizer::.ctor()
IL_0006: stfld class MySynchronizer MyBuilder1::m_synchronizer
IL_000b: ldarg.0
IL_000c: call instance void [mscorlib]System.Object::.ctor()
IL_0011: ret
} // end of method MyBuilder1::.ctor
位置-
class MyBuilder2
{
private MySynchronizer m_synchronizer;
public MyBuilder2()
{
m_synchronizer = new MySynchronizer();
}
}
给予我们
// Methods
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2063
// Code size 18 (0x12)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ldarg.0
IL_0007: newobj instance void MySynchronizer::.ctor()
IL_000c: stfld class MySynchronizer MyBuilder2::m_synchronizer
IL_0011: ret
} // end of method MyBuilder2::.ctor
区别只是订购之一:
- 字段初始值设定项(
MyBuilder1
)发生在基本类型构造函数调用之前(object
是这里的基础;调用实例无效[mscorlib] System.Object ::.ctor()
是基本构造函数调用) - 构造函数发生在 基本类型构造函数调用之后
- field initializers (
MyBuilder1
) happen before the base-type constructor call (object
is the base here;call instance void [mscorlib]System.Object::.ctor()
is the base-constructor call) - constructors happen after the base-type constructor call
在大多数情况下,这无关紧要.除非您的基本构造函数调用派生类型将覆盖的虚拟方法:否则,该字段在覆盖方法中是否具有值将在两者之间有所不同.
In most cases, this won't matter. Unless your base-constructor invokes a virtual method that the derived type overrides: then whether or not the field has a value in the overridden method will be different between the two.
这篇关于类声明v/s构造函数内部初始化对象的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!