Information Technology Reference
In-Depth Information
values. To satisfy the new() constraint, a class must have an explicit para-
meterless constructor. Therefore, you should create one so that clients can
use your type in generic classes or methods that enforce the new() con-
straint. That's not to say that every type needs a parameterless construc-
tor. However, if you support one, make sure to add the code so that the
parameterless constructor works in all cases, even when called from a
generic class with a new() constraint.
Yo u ' l l n o t e t h a t t h e s e c o n d c o n s t r u c t o r s p e c i f i e s " " f o r t h e d e f a u l t v a l u e
on the name parameter, rather than the more customary string.Empty.
That's because string.Empty is not a compile-time constant. It is a static
property defined in the string class. Because it is not a compile-time con-
stant, you cannot use it for the default value for a parameter.
However, using default parameters instead of overloads creates tighter cou-
pling between your class and all the clients that use it (see Item 10). In par-
ticular, the formal parameter name becomes part of the public interface,
as does the current default value. Changing parameter values requires a
recompile of all client code in order to pick up those changes. That makes
overloaded constructors more resilient in the face of potential future
changes. You can add new constructors, or change the default behavior for
those constructors that don't specify values without breaking client code.
C# versions 1 through 3 do not support default parameters, which is the
preferred solution to this problem. You must write each constructor that
you support as a separate function. With constructors, that can mean a lot
of duplicated code. Use constructor chaining, by having one constructor
invoke another constructor declared in the same class, instead of creating
a common utility routine. Several inefficiencies are present in this alter-
native method of factoring out common constructor logic:
public class MyClass
{
// collection of data
private List < ImportantData > coll;
// Name of the instance:
private string name;
public MyClass()
{
commonConstructor( 0 , "" );
}
 
Search WWH ::




Custom Search