
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Returning Multiple Items from a Method
|
163
specify otherwise. Therefore, the EnhancedLog<T> constructor contains an unseen call
(this call can be seen using Ildasm) to the default constructor of the
Log<T> class.
This problem can be solved in one of several ways. First, you could simply add a pro-
tected default constructor to the
Log<T> class. This would prevent the creation of a
Log<T> object using the default constructor, but would allow classes inheriting from
Log<T> to do so without problems. A second method is to use the base keyword to
direct the constructor to call a particular constructor in the base class. The following
EnhancedLog<T> class uses the base keyword to call the parameterized constructor of
the base
Log<T> class, passing in a StreamWriter object:
public class EnhancedLog<T> : Log<T>
where T : System.IO.TextWriter
{
public EnhancedLog (T logStream) : base(logStream)
{
// Initialize...
}
}
A third way to solve this problem is to make the Log<T> class noninheritable by add-
ing the
sealed keyword to the class declaration. While this prevents the problem of
calling the default constructor, it also prevents others from inheriting from and
extending the
Log<T> class. For many cases, this third solution is not the best one.
3.21 Returning Multiple Items from a Method
Problem ...