When the documentation speaks of a โkey collectionโ, they do not mean the same as the Dictionary. Note that there is actually a KeyedCollection base class: http://msdn.microsoft.com/en-us/library/ms132438%28v=vs.110%29.aspx
The key point is the following:
Unlike dictionaries, the KeyedCollection<TKey, TItem> element is not a key / value pair; instead, the entire element is a value, and the key is embedded in the value. For example, a collection item derived from KeyedCollection<String,String> (KeyedCollection(Of String, String) in Visual Basic) might be "John Doe Jr." where the meaning is "John Doe Jr." and the key is "Doe"; or collecting employee records containing integer keys can be obtained from KeyedCollection<int,Employee> . The abstract GetKeyForItem method retrieves a key from an element.
Thus, a collection with a key is a collection of objects, as well as a way to extract a key from each. Conceptually, it looks like a table in a database where you can define a primary key, which is a subset of the entire record.
Thus, bearing in mind, the answer becomes relatively clear. As others have said, hash code equality does not mean equality of objects. But keys in key form, such as primary keys in a database table, must uniquely identify the exact object. Thus, the possibility of hash collisions makes them unacceptable for this purpose.
In addition, even in the Dictionary there is an important difference between using objects as keys and using hash codes of the same objects as a key. If two objects have a hash collision but are not compared as equal, Dictionary will still save them as two separate keys. Therefore, overriding GetHashCode just for return 1 always works (although, obviously, it's not very good for performance). As a demonstration:
var dict = new Dictionary<MyClass, string>(); var hashDict = new Dictionary<int, string>(); dict[myObj1] = "One"; hashDict[myObj1.GetHashCode()] = "One"; dict[myObj2] = "Two"; hashDict[myObj2.GetHashCode()] = "Two"; Console.Out.WriteLine(dict[myObj1]);
( myObj1 and myObj2 are instances of MyClass that have the same hash code but are not compared as equal)