2013-07-31 18 views
0

我知道了,我要問一個愚蠢的問題,但我只是想知道在下面給出語句的區別:如何在提高性能的同時,在C#創建一個類的對象

Abc object= new ABC(); 
object.Age=obj1.Age; 
object.Place=obj1.Place; 
object.Street=obj1.Street; 
object.Number=obj1.Number; 
object.POBox=obj1.POBox; 

Abc object= new ABC() 
{ 
    Age=obj1.Age, 
    Place=obj1.Place, 
    Street=obj1.Street, 
    Number=obj1.Number, 
    POBox=obj1.POBox 
}; 

上述書面代碼是否有助於提高性能?我只想知道是否有任何方法可以在創建類的對象併爲該類對象分配值時提高性能?

+0

IL代碼將是相同的。這只是語法。 – wudzik

+7

_Technically_這是一個編譯器錯誤。只是說'' –

+0

@SimonWhitehead是的。美國廣播公司!=美國廣播公司,你不能聲明一個'對象' –

回答

4

不。這些語句被編譯爲相同的IL,因此沒有性能改進。

第一招:

IL_0031: newobj instance void TestApplication.ABC::.ctor() 
IL_0036: stloc.1 
IL_0037: ldloc.1 
IL_0038: ldc.i4.1 
IL_0039: callvirt instance void TestApplication.ABC::set_Age(int32) 
IL_003e: nop 
IL_003f: ldloc.1 
IL_0040: ldc.i4.1 
IL_0041: callvirt instance void TestApplication.ABC::set_Place(int32) 
IL_0046: nop 
IL_0047: ldloc.1 
IL_0048: ldc.i4.1 
IL_0049: callvirt instance void TestApplication.ABC::set_Street(int32) 
IL_004e: nop 
IL_004f: ldloc.1 
IL_0050: ldc.i4.1 
IL_0051: callvirt instance void TestApplication.ABC::set_Number(int32) 
IL_0056: nop 
IL_0057: ldloc.1 
IL_0058: ldc.i4.1 
IL_0059: callvirt instance void TestApplication.ABC::set_POBox(int32) 

,第二個:

IL_0001: newobj instance void TestApplication.ABC::.ctor() 
IL_0006: stloc.2 
IL_0007: ldloc.2 
IL_0008: ldc.i4.1 
IL_0009: callvirt instance void TestApplication.ABC::set_Age(int32) 
IL_000e: nop 
IL_000f: ldloc.2 
IL_0010: ldc.i4.1 
IL_0011: callvirt instance void TestApplication.ABC::set_Place(int32) 
IL_0016: nop 
IL_0017: ldloc.2 
IL_0018: ldc.i4.1 
IL_0019: callvirt instance void TestApplication.ABC::set_Street(int32) 
IL_001e: nop 
IL_001f: ldloc.2 
IL_0020: ldc.i4.1 
IL_0021: callvirt instance void TestApplication.ABC::set_Number(int32) 
IL_0026: nop 
IL_0027: ldloc.2 
IL_0028: ldc.i4.1 
IL_0029: callvirt instance void TestApplication.ABC::set_POBox(int32) 
相關問題