c# - List<T>.Remove 应该放在 List<T>.Exists 之前吗?

标签 c# .net list generics

拥有List<string> paths = new List<string>();我想删除我不确定是否存在的项目。我应该检查它是否存在,还是直接运行 Remove 方法?

if (paths.Exists(stringVar))paths.Remove(stringVar) 之前需要或考虑好的做法?

如果列表中没有这样的项目,则在没有 Exists 的情况下运行 Remove 将简单地返回 false。

最佳答案

不,它不会抛出异常,也不需要额外的检查。见MSDN :

true if item is successfully removed; otherwise, false. This method also returns false if item was not found in the List.

关于c# - List<T>.Remove 应该放在 List<T>.Exists 之前吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6175048/

相关文章:

c# - CompositionContainer.SatisfyImportsOnce 是否重用组件?

c# - 连接状态已打开但无效?

c# - 将 outerXml 转换为 OpenXmlElement

c# - 我应该始终使用 Task.Delay 而不是 Thread.Sleep 吗?

Haskell中的随机数列表

c# - 异步委托(delegate)操作澄清?

.net - 可能有相同的契约(Contract),相同的绑定(bind),相同的地址,但不同的端口?

c# - ASP.Net MVC 3 和 System.Data.Entity?

python - 将列表转换为字典,列表项作为键,列表索引作为 python 中的值

python - 使用要排除的索引列表进行索引