次のような 2 列のリストが必要です。
List<int,string> mylist= new List<int,string>();
それは言う
ジェネリック型を使用するには、
System.collection.generic.List<T>
1 つの型引数が必要です。
必要に応じて、いくつかのオプションがあります。
キー/値のルックアップを行う必要がなく、 に固執したい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);
不変の構造体を使用できます
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"));
あなたの例ではジェネリック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);
}
C#辞書データ構造を使用すると便利です...
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("one", 1);
dict.Add("two", 2);
簡単な方法で Ditionary からデータを取得できます。
foreach (KeyValuePair<string, int> pair in dict)
{
MessageBox.Show(pair.Key.ToString ()+ " - " + pair.Value.ToString () );
}
C# ディクショナリを使用したその他の例については... C# ディクショナリ
ナビ。
特定のシナリオについてはわかりませんが、次の 3 つのオプションがあります。
1.) Dictionary<..,..> を使用
する 2.) 値の周りにラッパー クラスを作成すると、List を使用できます
3.) Tuple を使用します
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]);
}
そのために、がキーであるDictionary
場所を使用できます。int
new Dictionary<int, string>();
本当にリストを使用したい場合はList<Tuple<int,string>>()
、Tuple
クラスは読み取り専用であるため、インスタンスを再作成して変更する必要があります。