InvalidOperationException
When an operation is carried out on an object that’s not in a valid state to do so, this exception type is raised. Sometimes this is due to not following a certain “protocol” that may be documented informally in the documentation for the type. For example, an object might need some kind of Initialize call before any other members can be called. As a concrete example, consider the SqlConnection class from System.Data.SqlClient:
using (SqlConnection conn = new SqlConnection(connString)) { Console.WriteLine(conn.ServerVersion);}
Here the ServerVersion property getter will throw the discussed exception because the connection hasn’t been opened yet by calling conn.Open().
This exception is also commonly thrown when enumerating ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access