【发布时间】:2013-10-14 10:00:54
【问题描述】:
我需要一个两列列表,例如:
List<int,string> mylist= new List<int,string>();
它说
使用泛型类型
System.collection.generic.List<T>需要 1 个类型参数。
【问题讨论】:
-
改用
List<custom class>。
我需要一个两列列表,例如:
List<int,string> mylist= new List<int,string>();
它说
使用泛型类型
System.collection.generic.List<T>需要 1 个类型参数。
【问题讨论】:
List<custom class>。
根据您的需要,这里有几个选项。
如果您不需要进行键/值查找并想坚持使用List<>,您可以使用Tuple<int, string>:
List<Tuple<int, string>> mylist = new List<Tuple<int, string>>();
// add an item
mylist.Add(new Tuple<int, string>(someInt, someString));
如果您确实需要键/值查找,您可以转向Dictionary<int, string>:
Dictionary<int, string> mydict = new Dictionary<int, string>();
// add an item
mydict.Add(someInt, someString);
【讨论】:
Tuple<,>,写mylist.Add(Tuple.Create(someInt, someString));会容易一些。
Tuple.Create(t1, t2) 在功能上等同于 new Tuple<t1, t2>(t1, t2),我个人更喜欢写作和阅读后者,因为它更明确。我不认为这是更多的工作 - 但我喜欢确切地看到需要什么=P
SortedDictionary<int, string>或SortedList<int, string>,不过要看他的用途。
不确定您的具体情况,但您有三个选择:
1.) 使用字典<..>
2.) 围绕您的值创建一个包装类,然后您可以使用 List
3.) 使用元组
【讨论】:
由于您的示例使用通用 List,我假设您不需要对数据使用索引或唯一约束。 List 可能包含重复值。如果您想确保唯一的密钥,请考虑使用Dictionary<TKey, TValue>()。
var list = new List<Tuple<int,string>>();
list.Add(Tuple.Create(1, "Andy"));
list.Add(Tuple.Create(1, "John"));
list.Add(Tuple.Create(3, "Sally"));
foreach (var item in list)
{
Console.WriteLine(item.Item1.ToString());
Console.WriteLine(item.Item2);
}
【讨论】:
为此,您可以使用Dictionary,其中int 是键。
new Dictionary<int, string>();
如果你真的想使用一个列表,它可能是一个List<Tuple<int,string>>(),但是Tuple 类是只读的,所以你必须重新创建实例来修改它。
【讨论】:
您可以使用 不可变 结构
public struct Data
{
public Data(int intValue, string strValue)
{
IntegerData = intValue;
StringData = strValue;
}
public int IntegerData { get; private set; }
public string StringData { get; private set; }
}
var list = new List<Data>();
或KeyValuePair<int, string>
using Data = System.Collections.Generic.KeyValuePair<int, string>
...
var list = new List<Data>();
list.Add(new Data(12345, "56789"));
【讨论】:
The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive.
List<T>/T[]。所以在我看来,这是一个比上课更好的建议。
struct 而不确保人们对差异有很好的理解是危险的。 (也看到有人试图编译像list[0].StringData = "test" 这样的东西是罕见的享受)。
使用 C# 字典数据结构对你有好处...
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("one", 1);
dict.Add("two", 2);
您可以通过简单的方式从字典中检索数据..
foreach (KeyValuePair<string, int> pair in dict)
{
MessageBox.Show(pair.Key.ToString ()+ " - " + pair.Value.ToString () );
}
更多使用 C# 字典的例子...C# Dictionary
导航。
【讨论】:
List<Tuple<string, DateTime, string>> mylist = new List<Tuple<string, DateTime,string>>();
mylist.Add(new Tuple<string, DateTime, string>(Datei_Info.Dateiname, Datei_Info.Datum, Datei_Info.Größe));
for (int i = 0; i < mylist.Count; i++)
{
Console.WriteLine(mylist[i]);
}
【讨论】:
<string, DateTime, string> - 他们想要<int, string>。虽然方法是一样的,但没有必要混淆这个问题。
使用来自 C# 7(VS 2017 及更高版本)的新 ValueTuple,有一个新的解决方案:
List<(int,string)> mylist= new List<(int,string)>();
这会创建一个ValueTuple 类型的列表。如果您的目标是 .NET Framework 4.7+ 或 .NET/.NET Core,则它是本机的,否则您必须获取 ValueTuple package from nuget。
这是一个与Tuple相反的结构,它是一个类。与Tuple 类相比,它还具有可以创建命名元组的优势,如下所示:
var mylist = new List<(int myInt, string myString)>();
这样你就可以像mylist[0].myInt和mylist[0].myString一样访问
【讨论】: