1

Which is generally considered the more preferred method when trying to add a constructor with an additional parameter? A subclass or a wrapper? That being, creating a subclass of the class and then just using that subclass' constructor? Or adding a wrapper method which will take the extra parameter and return an object with that parameter set?

Thank you for your time!

EDIT:

I don't have access to the superclass's code.

golmschenk
  • 11,736
  • 20
  • 78
  • 137

1 Answers1

1

The answer is language dependent. In C#/.NET, you would typically use an overloaded constructor:

public class Foo 
{
   private readonly string _greeting;

   public Foo() : this("Hello") { }

   public Foo(string greeting) {
     _greeting = greeting;
   } 

   //...
}
neontapir
  • 4,698
  • 3
  • 37
  • 52
  • Thanks, but do you happen to have a suggestion if the constructor can't be overloaded? – golmschenk Nov 14 '12 at 16:57
  • Again, it depends. Is the superclass sealed, or can you inherit from it? If you can inherit, I'd use subclassing like the `Stream` classes. If not, use a wrapper like `HttpContextBase` is for `HttpContext`. – neontapir Nov 14 '12 at 17:02