MyClass
, and that
MyClass
defines a parameterless
constructor. Thus, it is valid for use as a type argument for
Test
. It must be emphasized that
it was not necessary for
MyClass
to explicitly declare a parameterless constructor. Its default
constructor would also satisfy the constraint. However, if a class needs other constructors
in addition to a parameterless one, then it would be necessary to also explicitly declare a
parameterless version, too.
There are three important points about using
new( )
. First, it can be used with other
constraints, but it must be the last constraint in the list. Second,
new( )
allows you to
construct an object using only the parameterless constructor, even when other constructors
are available. In other words, it is not permissible to pass arguments to the constructor of a
type parameter. Third, you cannot use
new( )
in conjunction with a value type constraint,
described next.
Do'stlaringiz bilan baham: |