hoh*_*hmm 1 c# generics ienumerable
public static C RotateLeft<C, T>(C list, int count) where C : IEnumerable<T>
{
return list.Skip (count).Concat(list.Take(count));
}
Run Code Online (Sandbox Code Playgroud)
我希望得到类似这样的东西,其中T是IEnumerable的类型参数,C实现IEnumerable.这是我提出的语法,但它没有通过编译器.什么方式得到我想要的?谢谢!
你为什么不遗漏C-param?
public static IEnumerable<T> RotateLeft<T>(IEnumerable<T> list, int count)
{
return list.Skip (count).Concat(list.Take(count));
}
Run Code Online (Sandbox Code Playgroud)
编辑:正如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));
}
Run Code Online (Sandbox Code Playgroud)
然而,虽然这将解决您的编译器问题,但它不会让您得到您想要的,因为它InvalidCastException在尝试将结果Concat转换为实例时返回C.