C# 列出两个引用?

标签 c# list reference

我需要 2 个单独的列表和一个包含这两个列表项的列表。

List<int> list1 = new List<int>();
List<int> list2 = new List<int>();

List<int> list3 = list1 & list2

当我在 list1 中添加一些整数时,我希望它们也出现在 list3 中。 当一个新项目被添加到 list2 时,我想要相同的行为。

多个列表的引用。

这可能吗?

最佳答案

不,你不能用 List<T> 做到这一点直接地。但是,您可以声明:

IEnumerable<int> union = list1.Union(list2);

现在这将被延迟评估 - 每次迭代 union 时,它将返回 list1 中的每个整数或 list2 (或两者)。它只会返回任何整数一次。

如果你想要等价但带有串联,你可以使用

IEnumerable<int> concatenation = list1.Concat(list2);

同样,这将被惰性评估。

如评论中所述,这不会公开 List<T> 的所有操作确实如此,但如果您只需要从“组合整数”中读取(并以迭代方式而不是以某种随机访问方式读取),那么它可能就是您所需要的。

关于C# 列出两个引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28304888/

相关文章:

c# - 从 NHibernate 获取 {"could not execute batch command.[SQL: SQL not available]"} 错误

r - 组合 R 中的列表元素

c++ - 返回 const 引用与临时对象

c++ - 为什么 const 有意义?

c# - Windows 10 UWP,带有汉堡菜单的后退按钮到上一个框架

c# - 系统.Net.WebException : The underlying connection was closed: An unexpected error occurred on a receive

C++ 列表循环

python - 从列表中获取最后一项作为整数/ float

c# - 如何枚举给定根对象的所有可到达对象?

c# - 从 app.config 获取数据的最佳方式是什么?