如何从列表&LT删除INT []; INT []&GT ;? [英] How to remove int[] from List<int[]>?

查看:128
本文介绍了如何从列表&LT删除INT []; INT []&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用列表在C#中的数组是新的。所以我一直在使用它遇到问题。

I'm quite new in using List as arrays in C#. So I've encounter a problem while using it.

我试图卸下了 INT [] 整数数组的)从列表< INT [] > 使用删除,但未能删除 INT [] 列表与LT; INT []>

I'm trying to removed an int[] (integer array) from a List<int[]> using the Remove but failed to removed the int[] from the List<int[]>.

这里是code:

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 []

推荐答案

的问题是,每一个数组类型是引用类型和列表基于平等哪里平等删除项目对于引用类型是默认的参考平等。这意味着,你必须删除非常相同的阵列是在列表中。

The problem is that every array type is a reference type and List removes items based on equality where equality for reference types is by default reference equality. That means, you have to remove the very same array as is in the list.

例如下面工作得很好:

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

这篇关于如何从列表&LT删除INT []; INT []&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆