simple construct like the following
Is there anything in the C# which like the following <key, string, string>开发者_运维知识库;
which I can use key to access the second and third field quickly.
Since you have indicated that you are not using .NET 4, you will have to define a class or struct to hold the two strings you are interested in:
class Foo
{
public StringOne { get; set; }
public StringTwo { get; set; }
}
Then use a Dictionary<string, Foo>
, like so:
var dict = new Dictionary<string, Foo>();
dict["key"] = new Foo() {
StringOne = "Hello",
StringTwo = "World"
};
Don't forget to give the class and its properties some meaningful names.
Why not write this
class StringPair {
public string Item1 { get; set; }
public string Item2 { get; set; }
}
Dictionary<TKey, StringPair>
Would this work for you?
class Table<TKey, TValues>
{
Dictionary<TKey, int> lookup;
List<TValues[]> array;
public Table()
{
this.lookup = new Dictionary<TKey, int>();
this.array = new List<TValues[]>();
}
public void Add(TKey key, params TValues[] values)
{
array.Add(values);
lookup.Add(key, array.Count - 1);
}
public TValues[] this[TKey key]
{
get { return array[lookup[key]]; }
set { array[lookup[key]] = value; }
}
}
class Program
{
static void Main(string[] args)
{
Table<int, string> table = new Table<int, string>();
table.Add(10001, "Joe", "Curly", "Mo");
table.Add(10002, "Alpha", "Beta");
table.Add(10101, "UX-300", "UX-201", "HX-100b", "UT-910");
string[] parts = table[10101];
// returns "UX-300", "UX-201", "HX-100b" and "UT-910".
}
}
精彩评论