Last active
August 29, 2015 14:14
-
-
Save joncham/1900ab969cc34def30bb to your computer and use it in GitHub Desktop.
Accidental usage of IEnumerable.Contains rather than HashSet<T>.Contains
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace GenericConsoleTest | |
{ | |
class Base {} | |
class Derived : Base {} | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var hashset = new HashSet<Derived> (); | |
var derived = new Derived(); | |
hashset.Add(derived); | |
// I call HashTable.Contains | |
Console.WriteLine (hashset.Contains (derived)); | |
// I call Enumerable<Base>.Contains | |
// In .Net pre-4.0 this causes compiler error | |
Console.WriteLine (hashset.Contains ((Base)derived)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment