Consider the following class structure:
public class Foo<T>
{
public virtual void DoSomething()
{
}
public class Bar<U> where U : Foo<T>, new()
{
public void Test()
{
var blah = new U();
blah.DoSomething();
}
}
}
public class Baz
{
}
public class FooBaz : Foo<Baz>
{
public override void DoSomething()
{
}
}
When I go to use the nested class, I have something like the following:
var x = new FooBaz.Bar<FooBaz>();
It seems redundant to have to specify it twice. How would I create my class structure such that I can do this instead:
var x = new FooBaz.Bar();
Shouldn’t there be some way on the where clause of the nested class to say that U is always the parent? How?
Update: Added methods for DoSomething() above to address some of the comments. It’s important that when I call DoSomething, it addresses the overridden version. If I just use Foo instead of U, then the base implementation is called instead.
If
class Bardoes not need to be generic, why do you make it one?This would work:
And then
Of course all of this is totally abstract, so it might or might not apply to a practical example. What exactly are you trying to achieve here?