在C#中有什么东西可以像下面的<key, string, string>
那样我可以使用快捷键来访问第二个和第三个字段。简单构造如下
简单构造如下
回答
既然你已经表明你没有使用.NET 4,你必须定义一个类或结构保持两个字符串你有兴趣:
class Foo
{
public StringOne { get; set; }
public StringTwo { get; set; }
}
然后用Dictionary<string, Foo>
,像这样:
var dict = new Dictionary<string, Foo>();
dict["key"] = new Foo() {
StringOne = "Hello",
StringTwo = "World"
};
不要忘记给这个类及其属性一些有意义的名字。
不使用.net 4.0。找到一个名为keyValuedPair的类。 – user496949 2010-11-19 02:24:00
KeyValuePair不是你想要的;它旨在用于将键映射到值的字典和其他东西。请参阅我的更新的答案,这将与C#3和.NET 2.0/3.5一起使用。 – cdhowie 2010-11-19 02:24:38
看起来像只是对
为什么不写这个
class StringPair {
public string Item1 { get; set; }
public string Item2 { get; set; }
}
Dictionary<TKey, StringPair>
不喜欢这个简单任务的额外课程 – user496949 2010-11-19 02:25:58
你没有什么可以做的。抱歉。 – 2010-11-19 02:26:24
元组真的好多了。不幸的是,它仅在4.0版本中是 – user496949 2010-11-19 02:26:40
将这项工作吗?
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".
}
}
- 1. 简单的Java构造
- 2. ASP.NET中的简单HTML构造?
- 3. 使用一个简单的构造的
- 4. 简单的JavaScript对象构造函数
- 5. 在MVC中构造下拉菜单?
- 6. 如何简化多个构造函数?
- 7. Android - 如何构造一个简单的布局
- 8. 也比不上我有以下简单的拉姆达默认构造右值
- 9. 简单的重构例如
- 10. 简单的滑块造型
- 11. 简单重造在mercurial?
- 12. 简单的重构
- 13. 简单继承后调用复制构造函数
- 14. variadic模板类中的(简单)构造函数
- 15. 简单的C++构造函数不工作(很基本的)
- 16. 的一些简单的构造函数代码解释
- 17. 简单构造函数中的Flowtype错误
- 18. 从简单的arrayfire构造函数中获取seg fault
- 19. 简单的构造函数初始化列表
- 20. 简单的构造函数两难困境 - 继承
- 21. 在qooxdoo中导致错误的简单构造函数 - new qx.data.controller.Object();
- 22. 概念上简单的线性时间后缀树构造
- 23. 构造函数java试图做一个简单的程序
- 24. 简单的方法来声明所有父构造函数?
- 25. 语法或构造简化if()语句?
- 26. C++简化构造函数重载
- 27. 简单的方法来创造可能的情况下
- 28. 单和经典构造
- 29. PHP构造函数单PARAM
- 30. Sql Server Service Broker:如何构造简单队列场景的对话?
什么问题? – 2010-11-19 02:09:15
字典? –
2010-11-19 02:11:50