通常,如果所有列表都是字符串类型,我会使用:
var allLists = new[] { list1, list2, list3 };
bool result = allLists.All(l => l.Count == allLists[0].Count);
或等价物。但是,如果 List1
是 string
类型,List2
类型是 int
& List3
类型double
那么如何实现上述目标呢?
显然 Count
在尝试上述方法时不可用并且不想沿着以下路线走:
if(List1.Count == List2.Count && List2.Count == List3.Count etc.
最佳答案
好吧,你可以简单地使用:
IList[] allLists = { list1, list2, list3 };
请注意,这将向上转换所有 List<T>
非通用接口(interface)的实例。
示例:
var list1 = new List<int>();
var list2 = new List<string>();
var list3 = new List<double>();
IList[] allLists = {list1, list2, list3};
bool result = allLists.All(l => l.Count == allLists[0].Count);
关于c# - 比较多种类型的多个列表的计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39452484/