What is the correct .NET exception to throw when try to insert a duplicate object into a collection?

Why has InvalidOperationException been accepted as the answer?! It should be an ArgumentException?!

InvalidOperationException should be used if the object having the method/property called against it is not able to cope with the request due to uninit'ed state etc. The problem here is not the object being Added to, but the object being passed to the object (it's a dupe). Think about it, if this Add call never took place, would the object still function as normal, YES!

This should be an ArgumentException.


.Net will throw a System.ArgumentException if you try to add an item to a hashtable twice with the same key value, so it doesnt look like there is anything more specific. You may want to write your own exception if you need something more specific.


You should probably throw ArgumentException, as that is what the base library classes do.