26

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

Community
  • 1
  • 1
eugeneK
  • 9,954
  • 17
  • 64
  • 98

6 Answers6

30

Try List<Tuple<string, object>>. Then you can get the usual linq support over the list to get at each Tuple<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 object this 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 Lookup class (in your case, Lookup<string, object>), which would express your intent a little clearer than a List<Tuple<string, object>>.

Community
  • 1
  • 1
Adam Houldsworth
  • 60,104
  • 9
  • 137
  • 177
  • 1
    A caveat is that Tuple is not available in older versions of .Net - it was introduced in .Net 4 – dash Jul 10 '12 at 08:46
  • @dash True, but a Tuple can be implemented generically for older versions, many people had Tuple before it was baked in. Either way the question is tagged version agnostically, so I reserve the right to assume the latest :-) thanks for the clarification though. – Adam Houldsworth Jul 10 '12 at 08:47
  • 1
    The comment is for other people who see your answer - PowerCollections has had it for ever :-) – dash Jul 10 '12 at 08:48
  • Works like charm. Thanks – eugeneK Jul 10 '12 at 08:49
  • @dash Indeed, in fact I spent a day here at the office removing our own Tuple from the code-base in favour of the .NET one as of v4. – Adam Houldsworth Jul 10 '12 at 08:50
  • @AdamHouldsworth, Tuple works better for me. I don't need to check equality, just to loop over Collection – eugeneK Jul 10 '12 at 09:03
  • The Lookup class cannot be instantuated? – mhapps Mar 20 '18 at 15:46
10

you can use

List<KeyValuePair<string,object>>

or

Dictionary<string,List<object>>
Rzv.im
  • 888
  • 7
  • 12
4

Try to create your custom class and use it

    class KeyValue<Tkey,TValue>
    {
       public Tkey Key {get;set;}
       public TValue Value {get;set;}
    }

use like

List<KeyValue<string,string>> list = new List<KeyValue<string,string>>();
Cheng Chen
  • 39,413
  • 15
  • 105
  • 159
Govind Malviya
  • 12,709
  • 16
  • 65
  • 90
4

Maybe you could create a List of tuples using the Tuple class? Something like so:

List<Tuple<String, Object>> list = new List<Tuple<String, Object>>();
npinti
  • 50,175
  • 5
  • 67
  • 92
2

If you're using .NET 4, you can use the Tuple ( http://msdn.microsoft.com/en-us/library/system.tuple.aspx ) object:

var list = new List<Tuple<string, object>>();

if you're using an earlier version of .NET, you can implement the Tuple class yourself: Equivalent of Tuple (.NET 4) for .NET Framework 3.5

Community
  • 1
  • 1
D-loader
  • 51
  • 2
0

Have you considered using "Group by" then turned the grouped result into dictionary?

Tianzhen Lin
  • 1,846
  • 14
  • 15