Skip to content

Instantly share code, notes, and snippets.

@joncham
Last active August 29, 2015 14:14
Show Gist options
  • Save joncham/1900ab969cc34def30bb to your computer and use it in GitHub Desktop.
Save joncham/1900ab969cc34def30bb to your computer and use it in GitHub Desktop.
Accidental usage of IEnumerable.Contains rather than HashSet<T>.Contains
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