Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use generic parameter from another generic type

Tags:

c#

generics

Is it possible to use generic parameter from another generic type, and infer the usage where used like this:

public class TypeA<T2> { }

public class TypeB<T1, T3, T2> where T3 : TypeA<T2> {}

public class Test
{
    public Test()
    {
        // works but kind of ugly...
        new TypeB<string, TypeA<int>, int>();

        // Is it possible somehow to infer the TypeAType from the implemented generic generic type so I can use it like this?
        new TypeB<string, TypeA<int>>();
    }
}
like image 737
Rune Jensen Avatar asked Dec 17 '25 14:12

Rune Jensen


1 Answers

The short answer is no, because C# does not support "generics on generics".

To explain what that means let me show you are more concrete example:

List<int> list = new List<int>();
HashSet<int> set = list.To<HashSet<int>>();

Here, the extension method To() is a magical piece of code that can convert one type of container into another.

Assuming C# supported this feature, one would naively try to define that method like so:

public static class EnumerableExtensions {
    public static TContainer<TItem> To<TContainer<TItem>>(
        this IEnumerable<TItem> source)
        where TContainer: IEnumerable {
        // magic
    }
}

This of course won't compile, because C# cannot parse a generic type parameter that is generic itself (TContainer<TItem>).

This whole concept is called higher kinded polymorphism (or higher kinded types) which C# doesn't support. It might in the future though and you can track progress in this GitHub issue.

There is a Nuget package called LanguageExt which provides some higher-kinded types and if I recall correctly it does the same thing you do: explicitly declare the inner type so the type inference can use it.

like image 51
Good Night Nerd Pride Avatar answered Dec 20 '25 08:12

Good Night Nerd Pride



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!