I am trying to do the following but I think I must be missing something...(fairly new to generics)
(Need to target .NET 2.0 BTW)
interface IHasKey
{
string LookupKey { get; set; }
}
...
public static Dictionary<string, T> ConvertToDictionary(IList<T> myList) where T : IHasKey
{
Dictionary<string, T> dict = new Dictionary<string, T>();
foreach(T item in myList)
{
dict.Add(item.LookupKey, item);
}
return dict;
}
Unfortunately, this gives a "Constraints are not allowed on non-generic declarations" error. Any ideas?