【问题标题】:Make our own List<string, string, string>制作我们自己的 List<string, string, string>
【发布时间】:2012-05-15 09:16:19
【问题描述】:

我们可以在 C#.NET 中创建自己的List&lt;string, string, string&gt; 吗?我需要创建一个列表,其中包含 3 个不同的字符串作为列表中的单个元素。

【问题讨论】:

    标签: c# .net list


    【解决方案1】:

    您当然可以使用三个泛型类型参数创建自己的名为List 的类。我会强烈劝阻你不要这样做。这会让任何使用您的代码的人感到困惑。

    相反,要么使用List&lt;Tuple&lt;string, string, string&gt;&gt;(如果您使用的是.NET 4,无论如何)或(最好)创建自己的类以有意义的方式封装这三个字符串,然后使用@ 987654324@.

    创建您自己的类将使您在编写和阅读代码时更加清晰 - 通过为所涉及的三个不同字符串命名,每个人都会知道它们的含义。您还可以在需要时为类提供更多行为。

    【讨论】:

    • 如何从创建的元组列表中取回值?
    • @HotTester :取回值是什么意思?
    • @HotTester:使用Tuple&lt;,,&gt; 的属性——但它的可读性比创建自己的类要低得多。
    • @HotTester:是的,通过创建自己的类型。如果您坚决反对创建新类型,您可以(ab)use cast by example
    • @HotTester:这正是创建自己的类型(并可能赋予它行为等)的重点。你不能改变Tuple的属性,但是你可以用你自己的属性创建你自己的类...
    【解决方案2】:

    您可以使用Tuple 来实现。

    例如:

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

    这将返回最后一个元组。

    【讨论】:

      【解决方案3】:

      您可以使用tuples 的列表。像这样:

      List<Tuple<string, string, string>>
      

      【讨论】:

        【解决方案4】:

        可能是这样的:

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

        【讨论】:

          【解决方案5】:

          我推荐这个解决方案

           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",
                      });
                  }
              }
          

          【讨论】:

            【解决方案6】:

            例如:

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

            更多信息,请查看MSDN

            【讨论】:

            【解决方案7】:

            创建一个List&lt;Tuple&lt;string, string, string&gt;&gt; 怎么样?

            如果每个字符串都有特定的含义,则更好的想法可能是,将它们全部放入一个类中,然后创建一个列表。

            【讨论】:

              【解决方案8】:

              不,遗憾的是这不起作用。您可以做的最好的事情是创建一个列表列表,或者利用 IDictionary 接口的某种形式的实现。

              尽管如此,如果您有三项以这种方式属于一起的数据,那么创建一个包含它们的类可能是值得的。

              这将使您有机会在您的应用程序周围传递一个列表,并为每个数据项分配有意义的名称。永远不要低估六个月后可读性的力量。

              【讨论】:

              • 实际上,@Tigran 的解决方案不仅比我的好,它还教会了我一些我不知道的东西。不错。
              【解决方案9】:

              您可以定义一个 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&lt;Tuple&lt;string,string,string&gt;&gt;

              【讨论】:

                【解决方案10】:
                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");
                
                  }
                }
                

                【讨论】:

                  【解决方案11】:

                  有人试过dynamic 吗?

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

                  【讨论】:

                    猜你喜欢
                    • 2020-10-21
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-07-22
                    • 2017-05-08
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多