What would be the best C# data structure for using one key, and having two values pulled out?
Essentially I need a Dictionary<string, string, string>
. Is there something like this?
If you're using .NET 4, you could use
Dictionary<string, Tuple<string, string>>
If you're not, you could create your own Tuple
type which works the same way :)
Alternatively, if you only need this in one place you could create your own type which encapsulated the two strings neatly using appropriate names. For example:
public sealed class NameAndAddress
{
private readonly string name;
public string Name { get { return name; } }
private readonly string address;
public string Address { get { return address; } }
public NameAndAddress(string name, string address)
{
this.name = name;
this.address = address;
}
}
Then you can use:
Dictionary<string, NameAndAddress>
which makes it very clear what's going to be stored.
You could implement equality etc if you wanted to. Personally I would like to see this sort of thing made easier - anonymous types nearly do it, but then you can't name them...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With