Information Technology Reference
In-Depth Information
public MyClass( int initialCount)
{
commonConstructor(initialCount, "" );
}
public MyClass( int initialCount, string Name)
{
commonConstructor(initialCount, Name);
}
private void commonConstructor( int count,
string name)
{
coll = (count > 0 ) ?
new List < ImportantData >(count) :
new List < ImportantData >();
this .name = name;
}
}
That version looks the same, but it generates far less efficient object code.
The compiler adds code to perform several functions on your behalf in
constructors. It adds statements for all variable initializers (see Item 12).
It calls the base class constructor. When you write your own common util-
ity function, the compiler cannot factor out this duplicated code. The IL
for the second version is the same as if you'd written this:
public class MyClass
{
private List < ImportantData > coll;
private string name;
public MyClass()
{
// Instance Initializers would go here.
object (); // Not legal, illustrative only.
commonConstructor( 0 , "" );
}
public MyClass( int initialCount)
{
 
Search WWH ::




Custom Search