C# Generics Question

Posted by TheCloudlessSky on Stack Overflow See other posts from Stack Overflow or by TheCloudlessSky
Published on 2010-03-13T03:51:34Z Indexed on 2010/03/13 3:57 UTC
Read the original article Hit count: 254

Filed under:
|

Would it be possible to do something like the following in c#? Basically TParent and TChildren should be types of the class A but not necessairly have the same types that were passed in. I know this may sound confusing but I want to strongly type the children and parents of a particular object, but at the same time they must be of the same type. Because TParent inherits from A this would imply that it requires type parameters that inherit from A but using potentially different types.

public class A<TParent, TChildren> 
    where TParent : A
    where TControls : A
{
    TParent Parent;
    List<TChildren> Children;
}

or more easily seen here:

public class A<TParent, TChildren>
    where TParent : A<?, ?>
    where TChildren : A<?, ?>
{
    TParent Parent;
    List<TChildren> Children;
}

I hope this isn't too confusing. Is this at all possible?

Thanks :)

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics