Reputation: 3
When inheriting a method with a generic parameter in C#, what's the best way to handle cases where no parameter is actually required in the child class? For example, I'd like to do something like this:
public abstract class Parent {
protected abstract void Load<T>(T param);
}
public class Child : Parent {
protected override void Load() {}
}
If something like this isn't possible, is there a suggested standard practice for specifying T in cases where it's not actually required? Do I just set to something like bool and ignore the value?
child.Load<bool>(false);
Upvotes: 0
Views: 119
Reputation: 152521
When overriding a function you have to keep the same number (and types) of parameters. You can overload the parent method but not override it:
public abstract class Parent {
protected abstract void Load<T>(T param);
}
public class Child : Parent {
protected void Load() {}
protected override void Load<T>(T param) { // do nothing? }
}
However this is a code smell to me - making Load<T>
abstract essentially tells implementers that they must implement this method, but this class feels it does not need to do so. If code within Parent
expects Load<T>
to be properly overridden; how will it know that Child
implements some other flavor of Load
?
Upvotes: 2