9

List<string, string, string>C#.NET で独自のものを作成できますか? リスト内の 1 つの要素として 3 つの異なる文字列を持つリストを作成する必要があります。

4

11 に答える 11

30

List3 つのジェネリック型パラメーターで呼び出される独自のクラスを確実に作成できます。ただし、そうすることは強くお勧めしません。あなたのコードを使用している人を混乱させるでしょう。

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

独自のクラスを作成すると、コードを書いたり読んだりするときに、より明確になります。関連する 3 つの異なる文字列に名前を付けることで、誰もがその意味を理解できるようになります。必要に応じて、クラスにさらに多くの動作を与えることもできます。

于 2012-05-15T09:18:47.843 に答える
11

タプルを使用してそれを実現できます。

例えば:

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

これにより、最後のタプルが返されます。

于 2012-05-15T09:19:47.210 に答える
3

多分このようなもの:

var ls= new List<Tuple<string,string,string>>();
于 2012-05-15T09:19:10.597 に答える
3

タプルのリストを使用できます。そのようです:

List<Tuple<string, string, string>>
于 2012-05-15T09:18:31.667 に答える
3

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

 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",
            });
        }
    }
于 2012-05-15T09:42:28.353 に答える
2

例えば:

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

詳細については、MSDNを参照してください。

于 2012-05-15T09:33:51.300 に答える
1

を作ってList<Tuple<string, string, string>>みませんか?

ただし、各文字列に特定の意味がある場合は、それらすべてをクラスに入れ、その List を作成することをお勧めします。

于 2012-05-15T09:19:26.707 に答える
0

誰でも試しましたdynamicか?

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

var name = list[0].Name;
于 2016-07-28T07:22:52.147 に答える
0

いいえ、残念ながらこれは機能しません。あなたができる最善の方法は、リストのリストを作成するか、IDictionary インターフェイスの何らかの形式の実装を利用することです。

とはいえ、このように一緒に属する 3 つのデータ項目がある場合は、それらを含むクラスを作成する価値があるでしょう。

これにより、アプリケーションに List を渡して、各データ項目に意味のある名前を割り当てることができます。6 か月後の読みやすさの力を決して過小評価しないでください。

于 2012-05-15T09:23:41.353 に答える
0

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>>推奨されます。

于 2012-05-15T09:31:33.700 に答える
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");

  }
}
于 2012-05-15T09:33:06.183 に答える