C# 定义一个像 List<int,string> 这样的列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19231460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:27:59  来源:igfitidea点击:

define a List like List<int,string>?

c#.net

提问by Benny Ae

I need a two column list like:

我需要一个两列列表,如:

List<int,string> mylist= new List<int,string>();

it says

它说

using the generic type System.collection.generic.List<T>requires 1 type arguments.

使用泛型System.collection.generic.List<T>需要 1 个类型参数。

采纳答案by James

You could use an immutablestruct

您可以使用不可变结构

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>();

Or a KeyValuePair<int, string>

或者一个 KeyValuePair<int, string>

using Data = System.Collections.Generic.KeyValuePair<int, string>
...
var list = new List<Data>();
list.Add(new Data(12345, "56789"));

回答by newfurniturey

Depending on your needs, you have a few options here.

根据您的需要,您在此处有几个选择。

If you don't need to do key/value lookups and want to stick with a List<>, you can make use of Tuple<int, string>:

如果您不需要进行键/值查找并想坚持使用 a 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));

If you dowant key/value lookups, you could move towards a Dictionary<int, string>:

如果您确实想要键/值查找,您可以转向Dictionary<int, string>

Dictionary<int, string> mydict = new Dictionary<int, string>();

// add an item
mydict.Add(someInt, someString);

回答by walther

Not sure about your specific scenario, but you have three options:

不确定您的具体情况,但您有三个选择:

1.) use Dictionary<..,..>
2.) create a wrapper class around your values and then you can use List
3.) use Tuple

1.) 使用 Dictionary<..,..>
2.) 围绕您的值创建一个包装类,然后您可以使用 List
3.) 使用 Tuple

回答by andleer

Since your example uses a generic List, I assume you don't need an index or unique constraint on your data. A Listmay contain duplicate values. If you want to insure a unique key, consider using a Dictionary<TKey, TValue>().

由于您的示例使用 generic List,我假设您不需要对数据的索引或唯一约束。AList可能包含重复值。如果要确保唯一键,请考虑使用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);
}

回答by Samuel Ptheitroadier

For that, you could use a Dictionarywhere the intis the key.

为此,您可以使用Dictionarywhereint是关键。

new Dictionary<int, string>();

If you really want to use a list, it could be a List<Tuple<int,string>>()but, Tupleclass is readonly, so you have to recreate the instance to modifie it.

如果你真的想使用一个列表,它可能是一个List<Tuple<int,string>>()但是,Tuple类是只读的,所以你必须重新创建实例来修改它。

回答by Navib

Use C# Dictionary datastructure it good for you...

使用 C# 字典数据结构对你有好处......

Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("one", 1);
dict.Add("two", 2);

You can retrieve data from Ditionary in a simple way..

您可以通过一种简单的方式从 Ditionary 中检索数据。

foreach (KeyValuePair<string, int> pair in dict)
{
    MessageBox.Show(pair.Key.ToString ()+ "  -  "  + pair.Value.ToString () );
}

For more example using C# Dictionary... C# Dictionary

有关使用 C# 字典的更多示例... C# 字典

Navi.

导航。

回答by Hobelschlunze

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]);
}

回答by Magnetron

With the new ValueTuplefrom C# 7 (VS 2017 and above), there is a new solution:

使用ValueTupleC# 7的新版本(VS 2017 及更高版本),有一个新的解决方案:

List<(int,string)> mylist= new List<(int,string)>();

Which creates a list of ValueTuple type. If you're targeting .Net Framework 4.7+ or .Net Core, it's native, otherwise you have to get the ValueTuple package from nuget.

它创建了一个 ValueTuple 类型的列表。如果您的目标是 .Net Framework 4.7+ 或 .Net Core,则它是本机的,否则您必须从 nuget获取ValueTuple 包

It's a struct opposing to Tuple, which is a class. It also has the advantage over the Tupleclass that you could create a named tuple, like this:

它是一个与 相对的结构体Tuple,它是一个类。与Tuple类相比,它还具有可以创建命名元组的优势,如下所示:

var mylist = new List<(int myInt, string myString)>();

That way you can access like mylist[0].myIntand mylist[0].myString

这样你就可以访问像mylist[0].myIntmylist[0].myString