What causes 'invalidoperationexception in c#'?

Example:

List list = new List();
var enumerator = list.GetEnumerator();
list.Add(1);
int value = enumerator.Current;
Solution:

This exception indicates an operation isn't valid for the current state of the object. In the example, modifying a list while enumerating it causes this exception. Ensure the object is in the correct state before performing the operation.


List list = new List();
list.Add(1);
var enumerator = list.GetEnumerator();
int value = enumerator.Current;

Beginner's Guide to C#