bovlk
asked on
SortedDictionary<string, XYZ> to SortedDictionary<string, object>
Hello,
I have in C#, ASP.NET 3.5 the following code
public class ABC{
public class XYZ {
some implementation
}
}
Then I have a method that takes an arg of type SortedDictionary<string, object>. When I try to pass in SortedDictionary<string, ABC.XYZ>, I get the following compile error:
'System.Collections.Generi c.SortedDi ctionary<s tring,ABC. XYZ>' to 'System.Collections.Generi c.SortedDi ctionary<s tring,obje ct>'
Why? ABC.XYZ is of type object, isn't it? Any idea how to go around this in a clever way?
I have in C#, ASP.NET 3.5 the following code
public class ABC{
public class XYZ {
some implementation
}
}
Then I have a method that takes an arg of type SortedDictionary<string, object>. When I try to pass in SortedDictionary<string, ABC.XYZ>, I get the following compile error:
'System.Collections.Generi
Why? ABC.XYZ is of type object, isn't it? Any idea how to go around this in a clever way?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Still a question: why does not the SortedDictionary work?
ASKER
BTW, it was even simpler
public void MyMethod<T>(string s, int i, SortedDictionary<string, T> values)
{
}
works amazingly well.
public void MyMethod<T>(string s, int i, SortedDictionary<string, T> values)
{
}
works amazingly well.
You're welcome. :)
To explain to you. Generics have to of exactly the same type.
http://stackoverflow.com/questions/1431636/htmlhelperchildtype-not-assignable-to-htmlhelpermothertype
By moving them to single parameters the check is not:
SortedDictionary<x, y> vs SortedDictionary<x, z>
but rather
x vs x => ok
y vs z => when z is derived from y => ok
regards,
Kate
To explain to you. Generics have to of exactly the same type.
http://stackoverflow.com/questions/1431636/htmlhelperchildtype-not-assignable-to-htmlhelpermothertype
By moving them to single parameters the check is not:
SortedDictionary<x, y> vs SortedDictionary<x, z>
but rather
x vs x => ok
y vs z => when z is derived from y => ok
regards,
Kate
public void MyMethod<TKey, TValue>(SortedDictionary<T
regards,
Kate