Creating a Generic Dictionary From a Generic List?

You have not declared the generic parameter Change your declaration to: public static Dictionary ConvertToDictionary (IList myList) where T : IHasKey{ }.

You have not declared the generic parameter. Change your declaration to: public static Dictionary ConvertToDictionary (IList myList) where T : IHasKey{ }.

– Mikael Svenson Mar 3 '10 at 8:59 2 @Mikael: in this way the return value is a Dictionary instead of a Dictionary, so when you access the value you avoid an extra cast and are sure which objects your dictionary contains. – Paolo Tedesco Mar 3 '10 at 9:04 Good point! But if you were to think along the lines that the input list might contain different implementations of IHasKey, then boxing is unavoidable.

– Mikael Svenson Mar 3 '10 at 9:12 1 @Mikael: ok, but note that you could still use the same method with T=IHasKey. In any case I see no disadvantage in the generic approach. – Paolo Tedesco Mar 3 '10 at 9:30 I agree :) So it moves over to a philosophical question: Should you define it generically, when you know you will instantiate it with an interface and not a concrete type?

– Mikael Svenson Mar 3 '10 at 9:44.

Try something like this public class MyObject : IHasKey { public string LookupKey { get; set; } } public interface IHasKey { string LookupKey { get; set; } } public static Dictionary ConvertToDictionary(IList myList) where T: IHasKey { Dictionary dict = new Dictionary(); foreach(T item in myList) { dict. Add(item. LookupKey, item); } return dict; } List list = new List(); MyObject o = new MyObject(); o.

LookupKey = "TADA"; list. Add(o); Dictionary dict = ConvertToDictionary(list); You forgot the Generic Paramter in the method public static Dictionary ConvertToDictionary(IList myList) where T: IHasKey.

Since the classes in the input list are different (as you say in your comment) you can either implement it like suggested by @orsogufo, or you could just as well implement your signature on the interface itself: public static Dictionary ConvertToDictionary(IList myList) { var dict = new Dictionary(); foreach (IHasKey item in myList) { dict. Add(item. LookUpKey, item); } return dict; } Using the generic declaration is best if you have a list of one specific implementation of the interface as noted in the comments to the other answer.

Yes, but as orsogufo pointed out, this requires casting of IHasKey objects to access other members after retrieval from the dictionary. – Damien Mar 3 '10 at 9:40 But if the input list is of type IList which I assume it is since you have different classes in it, it makes no difference which implementation you use, since you would get an extra boxing for both if you wanted to do something with a specific class. – Mikael Svenson Mar 3 '10 at 9:43 The input list isn't necessarily IList but if it were you'd be right – Damien Mar 3 '10 at 10:27 @Mikael, the input list could be a IList where Customer : IHasKey.In that case, we get back a Dictionary instead of a Dictionary.

The Customer valued Dictionary is more useful (avoids needless casting). – David B Mar 3 '10 at 18:23 @David B: In scenarios where the List has one class type I agree 100%, but @Damien said in a comment that the list could contain several implementations of IHasKey (the way I understood it), and in that case it has to be a IList. Just wanted to get the actual scenario @Damien wanted to cover.

– Mikael Svenson Mar 3 '107 at 7:21.

You have not declared the generic parameter. Using the generic declaration is best if you have a list of one specific implementation of the interface as noted in the comments to the other answer. Terms of service.

Not the answer you're looking for?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions