c# - 比较多种类型的多个列表的计数?

标签 c# linq generics

通常,如果所有列表都是字符串类型,我会使用:

var allLists = new[] { list1, list2, list3 };
bool result = allLists.All(l => l.Count == allLists[0].Count);

或等价物。但是,如果 List1string 类型,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/

相关文章:

C#winform绑定(bind)源格式输出字符串到货币,不带$符号

c# - 第二个结果集返回空值 - linq toEntity

C# 使用 linq 反序列化 xml

c# - String.Split()方法: compiler chooses wrong overload

c# - Main() 不想访问类变量

android - 如何在Kotlin扩展功能中使用泛型

c# - 一般从 Entity Framework 返回一个项目

Java 泛型类型绑定(bind)到具有特定注释的类

C# CultureInfo.CurrentCulture 说 en_US 但我的 Windows 设置设置为南非

c# - Entity Framework 通用 DbSet 列表