[英]How would I convert one generic collection to another type of generic collection in c#
I am in an environment that contains a RollingWindow
class where RollingWindow<T>
can be a collection of any type/object. 我在一个包含RollingWindow
类的环境中,其中RollingWindow<T>
可以是任何类型/对象的集合。
I would like to create a C# method to convert a RollingWindow<T>
into a List<T>
. 我想创建一个C#方法将RollingWindow<T>
转换为List<T>
。
Essentially I'd use it in the following manner: 本质上,我将以以下方式使用它:
List<int> intList = new List<int>();
List<Record> = recordList = new List<Record>();
RollingWindow<int> intWindow = new RollingWindow<int>(20); //20 elements long
RollingWindow<Record> = recordWindow = new RollingWindow<Record>(10); //10 elements long
ConvertWindowToList(intList, intWindow); // will populate intList with 20 elements in intWindow
ConvertWindowToList(intList, intWindow); // will populate recordList with 10 elements in recordWindow
Any thoughts on how I would do this in c#? 关于如何在C#中执行此操作的任何想法?
Based on the assumption that RollingWindow<T>
implements IEnumerable<T>
then; 基于RollingWindow<T>
实现IEnumerable<T>
的假设;
List<int> intList = intWindow.ToList();
List<Record> recordList = recordWindow.ToList();
will work 将工作
I assume that the RollingWindow<T>
is derived from IEnumerable<T>
. 我假设RollingWindow<T>
是从IEnumerable<T>
派生的。
So this could be possible: 因此这是可能的:
var enumerableFromWin = (IEnumerable<int>) intWindow;
intList = new List<int>(enumerableFromWin);
var enumRecFromWin = (IEnumerable<Record>) recordWindow;
recordList = new List<Record>(enumRecFromWin);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.