从Collection中删除对象

Alo*_*lok 1 .net c# collections loops c#-4.0

我有一个对象集合,我希望根据函数调用删除一些对象将返回true或false.请参阅下面的示例代码snipette -

for each(MyClass obj in Objects)
{
  if(!ValidateObject(obj)
  { Objects.Remove(obj); }
}
Run Code Online (Sandbox Code Playgroud)

我不想在这里使用循环.上面的样品需要一个衬垫代码.提前致谢.

par*_*mar 5

如果您使用List,则带有谓词的RemoveAll方法将会执行

public int RemoveAll(
    Predicate<T> match
)
Run Code Online (Sandbox Code Playgroud)

Predicate(Of T)委托定义要删除的元素的条件.

 List<MyClass > f = new List<MyClass >();
 f.RemoveAll(x => ! ValidateObject(x) );
Run Code Online (Sandbox Code Playgroud)