dotnet/Documentation/compatibility/list_foreach-can-throw-exce...

30 lines
903 B
Markdown
Raw Normal View History

## List<T>.ForEach can throw exception when modifying list item
### Scope
Edge
### Version Introduced
4.5
### Source Analyzer Status
Available
### Change Description
Beginning in .NET 4.5, a `List<T>.ForEach` enumerator will throw an InvalidOperationException exception if an element in the calling collection is modified. Previously, this would not throw an exception but could lead to race conditions.
- [x] Quirked
- [ ] Build-time break
### Recommended Action
Ideally, code should be fixed to not modify lists while enumerating their elements because that is never a safe operation. To revert to the previous behavior, though, an app may target .NET 4.0.
### Affected APIs
* ``M:System.Collections.Generic.List`1.ForEach(System.Action{`0})``
### Category
Core
[More information](https://msdn.microsoft.com/en-us/library/hh367887\(v=vs.110\).aspx#core)
<!-- breaking change id: 5 -->