Steven Giesel 4/29/2024

How does a List know that you changed it while enumerating it?

Read Original

This technical article explores the internal mechanism in C# .NET's List<T> that triggers an InvalidOperationException when a list is modified while being enumerated. It demonstrates the concept by building a custom list class and shows how a 'version' field is incremented on mutations and checked during iteration to detect changes.

How does a List know that you changed it while enumerating it?

Comments

No comments yet

Be the first to share your thoughts!

Browser Extension

Get instant access to AllDevBlogs from your browser

Top of the Week

1
The Beautiful Web
Jens Oliver Meiert 2 votes
3
LLM Use in the Python Source Code
Miguel Grinberg 1 votes
4
Wagon’s algorithm in Python
John D. Cook 1 votes