List<string, string, string>
C#.NET で独自のものを作成できますか? リスト内の 1 つの要素として 3 つの異なる文字列を持つリストを作成する必要があります。
11 に答える
List
3 つのジェネリック型パラメーターで呼び出される独自のクラスを確実に作成できます。ただし、そうすることは強くお勧めしません。あなたのコードを使用している人を混乱させるでしょう。
代わりに、 (とにかく .NET 4 を使用している場合) を使用するかList<Tuple<string, string, string>>
、 (できれば) 独自のクラスを作成して、これらの 3 つの文字列を意味のある方法でカプセル化し、List<YourNewClass>
.
独自のクラスを作成すると、コードを書いたり読んだりするときに、より明確になります。関連する 3 つの異なる文字列に名前を付けることで、誰もがその意味を理解できるようになります。必要に応じて、クラスにさらに多くの動作を与えることもできます。
タプルを使用してそれを実現できます。
例えば:
var list = new List<Tuple<string,string,string>>();
単純に実行したい値を反復処理するには、次のようにします。
list.ForEach(x=>{
//x is a Tuple
});
または、特定のしなやかさを見つけるには、次のことを行うことをお勧めします。
var list = new List<Tuple<string,string,string>>{
new Tuple<string,string,string>("Hello", "Holla", "Ciao"),
new Tuple<string,string,string>("Buy", "--", "Ciao")
};
list.Where(x=>x.Item2 == "--");
これにより、最後のタプルが返されます。
多分このようなもの:
var ls= new List<Tuple<string,string,string>>();
タプルのリストを使用できます。そのようです:
List<Tuple<string, string, string>>
このソリューションをお勧めします
public class MyCustomClass
{
public string MyString1 { get; set; }
public string MyString2 { get; set; }
public string MyString3 { get; set; }
}
class MyApp
{
public MyApp()
{
List<MyCustomClass> customList = new List<MyCustomClass>();
customList.Add(new MyCustomClass
{
MyString1 = "Hello",
MyString2 = "Every",
MyString3 = "Body",
});
}
}
例えば:
var list = new List<Tuple<string, string, string>>();
var tuple = Tuple.Create("a", "b", "c");
list.Add(tuple);
詳細については、MSDNを参照してください。
を作ってList<Tuple<string, string, string>>
みませんか?
ただし、各文字列に特定の意味がある場合は、それらすべてをクラスに入れ、その List を作成することをお勧めします。
誰でも試しましたdynamic
か?
var list = new List<dynamic>();
list.Add(new { Name = "SampleN", Address = "SampleA", Email = "SampleE" });
var name = list[0].Name;
いいえ、残念ながらこれは機能しません。あなたができる最善の方法は、リストのリストを作成するか、IDictionary インターフェイスの何らかの形式の実装を利用することです。
とはいえ、このように一緒に属する 3 つのデータ項目がある場合は、それらを含むクラスを作成する価値があるでしょう。
これにより、アプリケーションに List を渡して、各データ項目に意味のある名前を割り当てることができます。6 か月後の読みやすさの力を決して過小評価しないでください。
List を定義し、必要なインターフェイス (IList など) を実装できます。コードブロー。
public class List<T1, T2, T3> : IList
{
#region IList Members
public int Add(object value)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(object value)
{
throw new NotImplementedException();
}
public int IndexOf(object value)
{
throw new NotImplementedException();
}
public void Insert(int index, object value)
{
throw new NotImplementedException();
}
public bool IsFixedSize
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public void Remove(object value)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
public object this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
#endregion
#region ICollection Members
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
public int Count
{
get { throw new NotImplementedException(); }
}
public bool IsSynchronized
{
get { throw new NotImplementedException(); }
}
public object SyncRoot
{
get { throw new NotImplementedException(); }
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
}
ただし、List<Tuple<string,string,string>>
推奨されます。
public class Listt< T, T1, T2>
{
public Listt()
{
}
public void Add(T item, T1 item1, T2 item3)
{
}
}
public partial class MyApp : Window
{
public MyApp()
{
InitializeComponent();
Listt<string, string, string> customList = new Listt<string, string, string>();
customList.Add("we","are","here");
}
}