2012-05-15 9 views

答えて

27

あなたは確かに3つのジェネリック型パラメータを持つListと呼ばれる独自のクラスを作成することができます。私は強くそうすることをお勧めします。それはあなたのコードを使用して誰かの混乱を混乱させるでしょう。

代わりに、いずれか使用List<Tuple<string, string, string>>は(とにかく、.NET 4を使用している場合)または(できれ)List<YourNewClass>を使用し、その後、有意義な方法でこれらの3つの文字列をカプセル化する独自のクラスを作成します。

独自のクラスを作成すると、コードを記述して読むときにはっきりとわかるようになります.3つの異なる文字列に名前を付けることで、誰もが意味するものを知ることができます。必要に応じて、クラスにもっと多くの振る舞いを与えることもできます。

+0

タプルのリストから値をフェッチバックする方法はありますか? – HotTester

+0

@HotTester:値をフェッチバックすることはどういう意味ですか? – Arion

+1

@HotTester: 'Tuple <,,>'のプロパティを使用してください。しかし、独自のクラスを作成するより読みにくくなります。 –

2

tuplesのリストを使用できます。これと同じように:

List<Tuple<string, string, string>> 
3

たぶん、このような何か:List<Tuple<string, string, string>>のことについてどのように

var ls= new List<Tuple<string,string,string>>(); 
1

それぞれの文字列に特定の意味がある場合は、それらをすべてクラスに入れてからそのリストを作成する方がよいでしょう。

10

これを実現するには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 == "--"); 

これは、最後のタプルを返します。

0

いいえ、残念ながら、これはうまくいきません。リストのリストを作成するか、IDictionaryインターフェースの実装の何らかの形を利用することができます。

このように、3つのデータが一緒に属していれば、それらを含むクラスを作成する価値はあると思います。

これにより、アプリケーションの周りにリストを渡して、各データ項目に意味のある名前を割り当てることができます。 6か月以内に読みやすさの強さを過小評価してはいけません。

+0

実際には、@ Tigranの解決策は私のものより優れているだけでなく、 。 いいです。 – inksmithy

0

リストを定義し、必要なインターフェイス(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>>を推奨します。例えば

0
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"); 

    } 
} 
2

:詳細については

var list = new List<Tuple<string, string, string>>(); 
var tuple = Tuple.Create("a", "b", "c"); 
list.Add(tuple); 

MSDNに見えます。

+1

リンクありがとうございます。ロシア語。私は "Россия(Pусский)"と読むことができます。 – felickz

+0

ロシア語を読むことができない人には...;) https://msdn.microsoft.com/library/system.tuple.aspx – GazB

4

私は誰もが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", 
      }); 
     } 
    } 
0

このソリューションをお勧めしますか?

var list = new List<dynamic>(); 
list.Add(new { Name = "SampleN", Address = "SampleA", Email = "SampleE" }); 

var name = list[0].Name;