See Also: InvalidOperationException Members
InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments. For example, InvalidOperationException is thrown by:
IEnumerator.MoveNext if objects of a collection are modified after the enumerator is created.
System.Resources.ResourceSet.GetString(string) if the resource set is closed before the method call is made.
To determine the precise cause of the exception, consult the exception information for the method call that throws the exception.
If the method invocation failure is due to invalid arguments, then ArgumentException or one of its derived classes, ArgumentNullException or ArgumentOutOfRangeException, should be thrown instead.
The System.Reflection.Emit.OpCodes.Ldflda Microsoft intermediate language (MSIL) instruction throws InvalidOperationException.
InvalidOperationException uses the HRESULT COR_E_INVALIDOPERATION, which has the value 0x80131509.
For a list of initial property values for an instance of InvalidOperationException, see the InvalidOperationException.#ctor constructors.
The following example demonstrates an error that causes a InvalidOperationException exception.
C# Example
using System; using System.Collections; public class InvalidOpExample { public static void Main() { int[] array = {0,0}; IEnumerator enumerator = array.GetEnumerator(); Console.Write("{0}",enumerator.Current); } }
The output is
Unhandled Exception: System.InvalidOperationException: Enumeration has not started. Call MoveNext.