开发者

C#, collection that holds Lists of different type

开发者 https://www.devze.com 2023-04-06 15:43 出处:网络
I currently have a class that holds 3 dictionaries, each of which contains Lists of the same type within each dictionary, but different types across the dictionaries, such as:

I currently have a class that holds 3 dictionaries, each of which contains Lists of the same type within each dictionary, but different types across the dictionaries, such as:

Dictionary1<string, List<int>> ... Dictionary2<string, List<double>>... Dictionary3<string, List<DateTime>>...

Is there a way to use a different collection that can hold all the Lists so that I can iterate through the collection of Lists? Being able to iterate is the only requirement of such collection, no sorting, no other operations will be needed.

I want to be able to access the List directly through the string or other identifier and access the List's members. Type safety is not a 开发者_C百科requirement but in exchange I do not want to have to cast anything, speed is the absolutely top priority here.

So, when calculations are performed on the list's members knowledge of the exact type is assumed, such as "double lastValue = MasterCollection["List1"].Last();", whereas it is assumed that List1 is a List of type double.

Can this be accomplished? Sorry that I may use sometimes incorrect or incomplete terminology I am not a trained programmer or developer.

Thanks, Matt


To do that you would have to use a non-generic API, such as IList (not IList<T>) - i.e. Dictionary<string, IList>. Or since you just need to iterate, maybe just IEnumerable (not IEnumerable<T>). However! That will mean that you are talking non-generic, so some sacrifices may be necessary (boxing of value types during retrieval, etc).

With an IList/IEnumerable appraoch, to tweak your example:

double lastValue = MasterCollection["List1"].Cast<double>().Last();

You could, of course, write some custom extension methods on IDictionary<string,IList>, allowing something more like:

double lastValue = MasterCollection.Get<double>("List1").Last();

I'm not sure it is worth it, though.


No, what you are trying to do is not possible; namely, the requirement for strong-typing on all of the lists without casting is what's preventing the rest.

If your only requirement is to iterate through each of the items in the list, then you could create your dictionary as a Dictionary<string, IEnumerable> (note the non-generic interface). IEnumerable<T> derives from IEnumerable which would allow you to iterate through each item in the list.

The problem with this is that you would have to perform a cast at some point either to the IEnumerable<T> (assuming you know you are working with it) or use the Cast<T> extension method on the Enumerable class (the latter being worse, as you might incur boxing/unboxing, unless it does type-sniffing, in which case, you wouldn't have a performance penalty).

I would say that you shouldn't store the items in a single list; your example usage shows that you know the type ahead of time (you are assigning to a double) so you are aware at that point in time of the specific typed list.

It's not worth losing type-safety over.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号