Speed of class instantiation (immediate assignment vs. default values)

  • Thread starter Thread starter Anders Borum
  • Start date Start date
A

Anders Borum

Hello!

When declaring variables without immediately assigning a value, the CLR
(please correct here) assigns a default value according to the (reference)
type of the variable (e.g. string = null, int = 0 etc.).

I always assign default values to my variables, but am unsure whether this
degrades the performance of the instantiation of my classes.

Are the following two classes as fast to instantiate?

public abstract class CmsObjectNode : CmsObject, IXml
{
private Guid cmsObjectID = Guid.Empty;
private string cmsName = null;
...
}

public abstract class CmsObjectNode : CmsObject, IXml
{
private Guid cmsObjectID;
private string cmsName;
...
}

I would assume so, as the variables - after class instantion - yield the
immediate assigned value.

Thanks in advance.
 
Anders... Inline
When declaring variables without immediately assigning a value, the
CLR(please correct here) assigns a default value according to the
(reference)type of the variable (e.g. string = null, int = 0 etc.).<

Not!

Class1 c1;
Class1 c2= null;
Class1 c3= new Class1();
// OK
Console.WriteLine(c3.ToString());
// Runtime error: System NullReferenceException
Console.WriteLine(c2.ToString());
// Compile time error: Use of unassigned local variable
Console.WriteLine(c1.ToString());
Are the following two classes as fast to instantiate?

public abstract class CmsObjectNode : CmsObject, IXml
{
private Guid cmsObjectID = Guid.Empty;
...
}

public abstract class CmsObjectNode : CmsObject, IXml
{
private Guid cmsObjectID;
...
<

In embedded C I may make a decision based on efficiency. In C# I would
favor the explicit initialization since the code is clear and easy to
read, efficiency be {return "What did the fish say when it hit the end
of the reservoir?;}

Regards,
Jeff
{Dam}:)
 
Hello!
In embedded C I may make a decision based on efficiency. In C# I would
favor the explicit initialization since the code is clear and easy to
read, efficiency be {return "What did the fish say when it hit the end
of the reservoir?;}

Great, I'll keep that in mind and continue what I'm doing. Again, which
version was the fastest? :-)

BTW. What did the fish say? ;-)
 
Anders... Here is the IL from a default declaration of an int, bool and
char:

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method ClassDefault::.ctor

Here is the IL from a initialization of an int, bool and char:

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 28 (0x1c)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: stfld int32 TestIL.ClassInit::i
IL_0007: ldarg.0
IL_0008: ldc.i4.1
IL_0009: stfld bool TestIL.ClassInit::b
IL_000e: ldarg.0
IL_000f: ldc.i4.7
IL_0010: stfld char TestIL.ClassInit::c
IL_0015: ldarg.0
IL_0016: call instance void [mscorlib]System.Object::.ctor()
IL_001b: ret
} // end of method ClassInit::.ctor

The first IL is faster.

Regards,
Jeff
Again, which version was the fastest? :-)<
{.00186 milliseconds faster actually}:)
 
Back
Top