C# 将泛型类型作为泛型类型参数传递?

C# pass generic type as a generic type parameter?

public static C RotateLeft<C, T>(C list, int count) where C : IEnumerable<T>
{
    return list.Skip (count).Concat(list.Take(count));
}

我想实现这样的目标,其中 T 是 IEnumerable 的类型参数,C 实现 IEnumerable。这是我想出的语法,但它没有通过编译器。有什么办法可以得到我想要的吗?谢谢!

你为什么不完全省略 C 参数?

public static IEnumerable<T> RotateLeft<T>(IEnumerable<T> list, int count)
{
    return list.Skip (count).Concat(list.Take(count));
}

编辑:正如 Suresh Kumar Veluswamy 已经提到的,您也可以简单地将结果转换为 C:

的实例
public static C RotateLeft<C, T>(C list, int count) where C : IEnumerable<T>
{
    return (C) list.Skip(count).Concat(list.Take(count));
}

然而,虽然这将解决您的编译器问题,但在尝试将 Concat 的结果转换为 returns 和 InvalidCastException 时,它不会让您得到想要的东西C.

的实例