[英]How would I convert one generic collection to another type of generic collection in c#
我在一個包含RollingWindow
類的環境中,其中RollingWindow<T>
可以是任何類型/對象的集合。
我想創建一個C#方法將RollingWindow<T>
轉換為List<T>
。
本質上,我將以以下方式使用它:
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
關於如何在C#中執行此操作的任何想法?
基於RollingWindow<T>
實現IEnumerable<T>
的假設;
List<int> intList = intWindow.ToList();
List<Record> recordList = recordWindow.ToList();
將工作
我假設RollingWindow<T>
是從IEnumerable<T>
派生的。
因此這是可能的:
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.