[Mono-dev] [PATCH] Fix HashSet<T> when used with nulls and non-default comparer

Tiaan Geldenhuys tagdev at gmail.com
Thu Feb 11 21:39:24 EST 2010


This patch is for System.Collections.Generic.HashSet<T> to fix the error
shows at the bottom, which happens when you create a hash-set with a
non-default comparer like StringComparer.Ordinal and then attempt to
populate it with a null value.  The hash-set should always be able to store
null values (for data-types that allow them), even if the underlying
comparer behaves according to spec (i.e., throws an ArgumentNullException
from IEqualityComparer<T>.GetHashCode for nulls).  The solution is simply to
bypass the comparer whenever a null value is encountered.

Please review carefully before committing.  (I hope those long expressions
are right -- they seem to be though.)

Thanks,
Tiaan.

---------------

[System.ArgumentNullException]: Argument cannot be null.
Parameter name: s
at System.OrdinalComparer.GetHashCode (string) <0x00066>
at System.Collections.Generic.HashSet`1<string>.GetItemHashCode (string)
<0x00033>
at System.Collections.Generic.HashSet`1<string>.Add (string) <0x00016>
at System.Collections.Generic.HashSet`1<string>..ctor
(System.Collections.Generic.IEnumerable`1<string>,System.Collections.Generic
.IEqualityComparer`1<string>) <0x000e4>
at
MyClass.MyMethodThatCreatesTheHashSetFromDataContainingTheNullStringAndUsing
StringComparerOrdinal (string) <0x00214>

===============



-------------- next part --------------
A non-text attachment was scrubbed...
Name: HashSet_trunk.patch
Type: application/octet-stream
Size: 1261 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20100211/273eab0f/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HashSetTest_trunk-or-branch-2-6.patch
Type: application/octet-stream
Size: 2179 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20100211/273eab0f/attachment-0001.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HashSet_branch-2-6.patch
Type: application/octet-stream
Size: 1261 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20100211/273eab0f/attachment-0002.obj 


More information about the Mono-devel-list mailing list