What’s wrong with the following code?
var names = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
This code is intended to check whether the result of a SQL query is contained in a case-insensitive collection of names. However, if you run this code, the resulting check will be case-sensitive. Why?
As you may have guessed from the title, this is caused by covariance. In fact, this code will not compile at all against .Net 3.5.
The problem is that
string. Therefore, it doesn’t call
HashSet<string>.Contains(string), which is what it’s intending to call (and which uses the HashSet’s comparer). Instead, on .Net 4.0, this calls the
Enumerable.Contains<object>(IEnumerable<object>, string) extension method, using the covariant conversion from
IEnumerable<object>. Covariance allows us to pass
object to the
Contains method of any strongly-typed collection (of reference types).
Still, why is it case-sensitive? As Jon Skeet points out, the LINQ Contains() method is supposed to call any built-in Contains() method from ICollection<T>, so it should still use the HashSet’s case-insensitive Contains().
The reason is that although HashSet<String> implements ICollection<string>, it does not implement ICollection<object>. Since we’re calling Enumerable.Contains<object>, it checks whether the sequence implements ICollection<object>, which it doesn’t. (ICollection<T> is not covariant, since it allows write access)
Fortunately, there’s a simple fix: just cast the return value back to string (and add a comment explaining the problem). This allows the compiler to call
HashSet<string>.Contains(string), as was originally intended.
//Call HashSet<string>.Contains(string), not the
//covariant Enumerable.Contains(IEnumerable<object>, object)
(I discovered this issue in my StringListConstraint for ASP.Net MVC
Republished with author's permission. See the original post here.
Republished from SLaks.Blog [36 clicks].
Read the original version here [32134 clicks].