Why do I get a compile error in the following code (see line with comment)?
public void Test()
{
HashSet<HashSet<Animal>> setWithSets = new HashSet<HashSet<Animal>>();
HashSet<Cat> cats = new HashSet<Cat>();
setWithSets.Add(cats); // Compile error
}
private class Animal { }
private class Cat : Animal { }
VS2012 gives me two errors, the first one the important one:
- Error 2 Argument 1: cannot convert from 'System.Collections.Generic.HashSet<Expenses.Tests.TestDb.SetTest.Cat>' to 'System.Collections.Generic.HashSet<Expenses.Tests.TestDb.SetTest.Animal>'
- Error 1 The best overloaded method match for 'System.Collections.Generic.HashSet<System.Collections.Generic.HashSet<Expenses.Tests.TestDb.SetTest.Animal>>.Add(System.Collections.Generic.HashSet)' has some invalid arguments
My question is: Why can I not add "cats" to the "setWithSets"?