Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I tried to declare a dictionary have a string as the key and an object a the value. I declared it with the help of the interfaces. This is my code
C#
ICollection<KeyValuePair<string, ICollection<DemoProperty>>> Test = new Dictionary<string, List<DemoProperty>>();


Its getting the coversion Error.

But there is no Error when I declare as below
C#
ICollection<KeyValuePair<string, ICollection<DemoProperty>>> Test = new List<KeyValuePair<string, ICollection<DemoProperty>>>();


and I know this can declare as below also
C#
Dictionary<string, ICollection<DemoProperty>> Test = new Dictionary<string, ICollection<DemoProperty>> 


What is the exact error on the first statement and how can I proceed with the first statatement. Hope you all understand my questions.
Posted
Updated 1-Jun-15 0:39am
v2
Comments
BillWoodruff 1-Jun-15 15:31pm    
Why are you using ICollection ?

What did you expect in the first example: you are using signatures that don't match.

You cannot recast the value type at declaration of the dictionary:

C#
ICollection<keyvaluepair><string,>>> Test = new Dictionary<string,>>();

Test.Add(new KeyValuePair<string,>>("a",new List<demoproperty>()));

</demoproperty></keyvaluepair>


If you could then you would not be able to add any ICollection, only List<demoproperty> which is not what your declaration states


You can instantiate an ICollection<x> as a Dictionary<x> because Dictionary<tkey,tvalue>; inherits ICollection<tkey,tvalue> but if you change the generic types then they won't match.
 
Share this answer
 
v3
In your first declaration you use List instead of ICollection. This works:
C#
ICollection<KeyValuePair<string, ICollection<DemoProperty>>> Test = new Dictionary<string, ICollection<DemoProperty>>();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900