Possible Duplicate:
Duplicate keys in .NET dictionaries?
I need to create a Collection<string,object>, one is a string and another one is an object.
I cannot use Dictionary since none of items is unique.
Is there any way to create List<T,T> ? Since i don’t want to create an object to hold both values just to use it in single foreach loop.
Thanks
Try
List<Tuple<string, object>>. Then you can get the usual linq support over the list to get at eachTuple<string, object>.The
Tuple<,>class is available as of .NET 4 onwards, but you can easily mimick a tuple type of your own without too much hassle.I think that tuples are considered equal based on their item values, but with
objectthis would likely be reference equals and thus shouldn’t likely be equal… if that made any sense!More info on equality here:
Surprising Tuple (in)equality
Update:
If you need to associate a list of items with a key for the purposes of a lookup, the duplicate question answer correctly highlights the
Lookupclass (in your case,Lookup<string, object>), which would express your intent a little clearer than aList<Tuple<string, object>>.