user584018
user584018

Reputation: 11354

Is there any alternate similar to NameValueCollection collection in C#

NameValueCollection can hold multiple string values under a single key.

Is there any collection where I can hold multiple OBJECT (not string) under a single key?

Upvotes: 2

Views: 713

Answers (2)

mjwills
mjwills

Reputation: 23975

You should consider Lookup<TKey, TElement> if you don't need mutability - https://msdn.microsoft.com/en-us/library/bb460184(v=vs.110).aspx (via ToLookup).

It also means you don't need to check for key existence - https://msdn.microsoft.com/en-us/library/bb292716(v=vs.110).aspx :

If the key is not found in the collection, an empty sequence is returned.

Upvotes: 2

meziantou
meziantou

Reputation: 21367

You can have a look at the MultiDictionary in the corefxlab repository. Install the Microsoft.Experimental.Collections Nuget:

MultiDictionary<string, int> myDictionary = new MultiDictionary<string, int>();
myDictionary.Add("key", 1);
myDictionary.Add("key", 2);
myDictionary.Add("key", 3);
//myDictionary["key"] now contains the values 1, 2, and 3

You can read the following post about the MultiDictionary: https://blogs.msdn.microsoft.com/dotnet/2014/06/20/would-you-like-a-multidictionary/

Upvotes: 3

Related Questions