Per SynchronizedCollection.Add
, an ArgumentException is thrown when "The value set is null or is not of the correct generic type T for the collection". ReSharper 8 also considers this a [NotNull] parameter.
However, there is no exception when running the following code in LINQPad:
new SynchronizedCollection<string>().Add((string)null);
Furthermore, inspection of SynchronizedCollection.Add reveals it delegates down to List.Insert
which does not have this limitation. ("The object to insert. The value can be null for reference types."
Given this conflicting information and behavior, is it valid to add null
to a SynchronizedCollection?
Did it used to be the case that adding a null
value would have raised an exception?
It is a documentation error. The exception only happens on
IList.Add(object)
(or any otherIList
method) not the normalAdd(T)
.Also the error only happens if
T
is a value type and you pass in null, it does not happen for non value types.From the Reference Source
If you look at the error info
That situation is not possible with
Add(T)
you will get a compiler error instead of a runtime error. You could only pass in a non correct generic type if the parameter was of typeobject
to get the run-time exception ofArgumentException
.