Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 移除所有不工作的元件_C#_List_Collections - Fatal编程技术网

C# 移除所有不工作的元件

C# 移除所有不工作的元件,c#,list,collections,C#,List,Collections,我在一个.NET项目中注意到了这个功能 private static void RemoveAllElements(ref List<int> listToBeRemoved) { foreach (var i in listToBeRemoved) { listToBeRemoved.Remove(i); } } private static void remove allelemen

我在一个.NET项目中注意到了这个功能

    private static void RemoveAllElements(ref List<int> listToBeRemoved)
    {
        foreach (var i in listToBeRemoved)
        {
            listToBeRemoved.Remove(i);
        }
    }
private static void remove allelements(ref List listToBeRemoved)
{
foreach(listToBeRemoved中的var i)
{
删除列表。删除(i);
}
}

这是从列表中删除所有元素的最快方法吗?我还注意到这个函数没有捕获任何异常。我应该换这个吗?这是在现有代码中。

我不明白为什么您不能将其简化为

listToBeRemoved.Clear();
有关更多详细信息,请参见

我认为您不需要添加任何异常处理逻辑。
Clear
方法在内部使用了
Array.Clear
,它作为成功和不会损坏状态的可靠性契约。我无法想象这会引发异常

list.Clear()
请参阅文档:

更改正在枚举的集合会使枚举器无效,因此这是一种非常糟糕的做法。见以下备注:

只要集合保持不变,枚举数就保持有效 不变。如果对集合进行了更改,例如添加, 修改或删除元素时,枚举数无法恢复 无效,其行为未定义

Clear
方法本身不会引发异常,但是如果
listToBeRemoved
null
,您将得到一个
NullReferenceException

内置方法如何

您的
RemoveAllElements
方法不仅完全没有必要,而且根本不起作用

运行以下代码将生成一个带有消息“Collection was modified;enumeration operation may not execute”的
InvalidOperationException

var list=新列表{1,2,3,4,5,6,7,8,9,0};
list.removalelements();
为什么不:

listToBeRemoved.Clear();

最快的方法是
listToBeRemoved.Clear()

此外,该方法没有理由使用
ref
,因为它对参数引用(仅对参数引用的列表)没有任何作用。您可能无法使用现有代码更改此设置,因为删除
ref
需要在调用位置进行更改。

static void Main(){
static void Main (){
List<string> words = new List<string>();
words.AddRange(new[] { "banana", "plum", "peach" });

condition = words.Count;
for (int i = 0; i < condition; i++) words.RemoveAt(0);}
//it returns empty list
列表单词=新列表(); AddRange(新[]{“香蕉”、“李子”、“桃子”}); 条件=字数。计数; 对于(inti=0;i
静态无效主管道()
{
列表单词=新列表();
AddRange(新[]{“香蕉”、“李子”、“桃子”});
words.RemoveAll(s=>s.Any());//此代码返回空列表
}

这是否有效?我不认为你可以像那样修改循环中的列表。这根本不起作用。它将抛出
invalidoOperationException
,因为您在迭代时修改了集合。它在代码中。我必须检查它是否被调用。有趣的是,在这个解决方案中,我根本看不到它被调用。那么,在这种情况下,由于该方法标记为
private
,您可以非常确定可以从类中删除整个方法。这个问题要求提供从列表中删除所有元素的最快/最简单的方法。你能解释一下为什么你认为你的答案与之前的答案有很大不同吗?不,这不是最快的方法,但只是其中一种可能性。当然,从列表中删除所有项目的最快方法是使用Clear()方法。我认为您需要解释在哪种情况下您应该更喜欢您的解决方案。否则就不清楚你为什么要发布它。
static void Main (){
List<string> words = new List<string>();
words.AddRange(new[] { "banana", "plum", "peach" });

condition = words.Count;
for (int i = 0; i < condition; i++) words.RemoveAt(0);}
//it returns empty list
static void Main ()
{
    List<string> words = new List<string>();
    words.AddRange(new[] { "banana", "plum", "peach" });
    words.RemoveAll(s=>s.Any());//This code returns empty list
 }