C#.NETでList<string, string, string>
を独自に作成できますか?私はリスト内の単一の要素として3つの異なる文字列を持つリストを作成する必要があります。独自のリストを作成する<string、string、string>
答えて
あなたは確かに3つのジェネリック型パラメータを持つList
と呼ばれる独自のクラスを作成することができます。私は強くそうすることをお勧めします。それはあなたのコードを使用して誰かの混乱を混乱させるでしょう。
代わりに、いずれか使用List<Tuple<string, string, string>>
は(とにかく、.NET 4を使用している場合)または(できれ)List<YourNewClass>
を使用し、その後、有意義な方法でこれらの3つの文字列をカプセル化する独自のクラスを作成します。
独自のクラスを作成すると、コードを記述して読むときにはっきりとわかるようになります.3つの異なる文字列に名前を付けることで、誰もが意味するものを知ることができます。必要に応じて、クラスにもっと多くの振る舞いを与えることもできます。
tuplesのリストを使用できます。これと同じように:
List<Tuple<string, string, string>>
たぶん、このような何か:List<Tuple<string, string, string>>
のことについてどのように
var ls= new List<Tuple<string,string,string>>();
?
それぞれの文字列に特定の意味がある場合は、それらをすべてクラスに入れてからそのリストを作成する方がよいでしょう。
これを実現するにはTupleを使用できます。
var list = new List<Tuple<string,string,string>>();
が値を反復するためにあなたがシンプルやってをしたいことがあります。例えば:
list.ForEach(x=>{
//x is a Tuple
});
またはにはいくつかの特定のタプルを見つけ、あなたはをすることができfolowingを行う:
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 == "--");
これは、最後のタプルを返します。
いいえ、残念ながら、これはうまくいきません。リストのリストを作成するか、IDictionaryインターフェースの実装の何らかの形を利用することができます。
このように、3つのデータが一緒に属していれば、それらを含むクラスを作成する価値はあると思います。
これにより、アプリケーションの周りにリストを渡して、各データ項目に意味のある名前を割り当てることができます。 6か月以内に読みやすさの強さを過小評価してはいけません。
実際には、@ Tigranの解決策は私のものより優れているだけでなく、 。 いいです。 – inksmithy
リストを定義し、必要なインターフェイス(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");
}
}
私は誰もがdynamic
を試してみました
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<dynamic>();
list.Add(new { Name = "SampleN", Address = "SampleA", Email = "SampleE" });
var name = list[0].Name;
タプルのリストから値をフェッチバックする方法はありますか? – HotTester
@HotTester:値をフェッチバックすることはどういう意味ですか? – Arion
@HotTester: 'Tuple <,,>'のプロパティを使用してください。しかし、独自のクラスを作成するより読みにくくなります。 –