gpt4 book ai didi

c# - 如何从 List 中删除 int[]?

转载 作者:行者123 更新时间:2023-11-30 13:30:38 26 4
gpt4 key购买 nike

我对在 C# 中将 List 用作数组还很陌生。所以我在使用的过程中遇到了问题。

我正在尝试删除 int[] (整数数组)来自 List<int[]>使用 Remove但未能删除 int[]来自 List<int[]> .

代码如下:

List<int[]> trash = new List<int[]>()
{
new int[] {0,1},
new int[] {1,0},
new int[] {1,1}
};
int[] t1 = {0,1};
trash.Remove(t1);

这只是一个错误吗?或者它无法识别 int[]

最佳答案

问题在于每个数组类型都是引用类型,List 根据相等性删除项目,其中引用类型的相等性默认为引用相等性。这意味着,您必须删除与列表中完全相同的数组。

下面的例子效果很好:

int[] t1 =  {0,1};
List<int[]> trash = new List<int[]>()
{
t1,
new int[] {1,0},
new int[] {1,1}
};
trash.Remove(t1);

关于c# - 如何从 List<int[]> 中删除 int[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30997483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com