【问题标题】:Quick way to create a list of values in C#?在 C# 中创建值列表的快速方法?
【发布时间】:2010-10-17 21:59:44
【问题描述】:

我正在寻找一种在 C# 中创建值列表的快速方法。在 Java 中,我经常使用下面的 sn-p:

List<String> l = Arrays.asList("test1","test2","test3");

除了下面显而易见的之外,C# 中还有其他等价物吗?

IList<string> l = new List<string>(new string[] {"test1","test2","test3"});

【问题讨论】:

    标签: c# list


    【解决方案1】:

    查看 C# 3.0 的 Collection Initializers

    var list = new List<string> { "test1", "test2", "test3" };
    

    【讨论】:

      【解决方案2】:

      如果您希望减少混乱,请考虑

      var lst = new List<string> { "foo", "bar" };
      

      这使用了 C# 3.0 的两个特性:类型推断(var 关键字)和列表的集合初始值设定项。

      或者,如果您可以使用数组,这甚至更短(少量):

      var arr = new [] { "foo", "bar" };
      

      【讨论】:

      • 我通常不会根据需要较少字符来初始化的数据结构来选择我的数据结构...
      • @Harrichael 是的,我也不是。但除非你需要一个列表,否则没有理由使用一个。
      【解决方案3】:

      在 C# 3 中,您可以:

      IList<string> l = new List<string> { "test1", "test2", "test3" };
      

      这使用了 C# 3 中新的集合初始化器语法。

      在 C# 2 中,我只会使用您的第二个选项。

      【讨论】:

      • Nitpick:集合初始化器,而不是对象初始化器。
      【解决方案4】:
      IList<string> list = new List<string> {"test1", "test2", "test3"}
      

      【讨论】:

        【解决方案5】:

        你可以这样做

        var list = new List<string>{ "foo", "bar" };
        

        以下是其他常见数据结构的一些其他常见实例:

        字典

        var dictionary = new Dictionary<string, string> 
        {
            { "texas",   "TX" },
            { "utah",    "UT" },
            { "florida", "FL" }
        };
        

        数组列表

        var array = new string[] { "foo", "bar" };
        

        队列

        var queque = new Queue<int>(new[] { 1, 2, 3 });
        

        堆栈

        var queque = new Stack<int>(new[] { 1, 2, 3 });
        

        正如您所看到的,在大多数情况下,它只是在花括号中添加值,或者实例化一个新数组,然后是花括号和值。

        【讨论】:

          【解决方案6】:

          您可以删除new string[] 部分:

          List<string> values = new List<string> { "one", "two", "three" };
          

          【讨论】:

            【解决方案7】:

            您可以在 C# 中使用 collection initialiser 稍微简化这行代码。

            var lst = new List<string> {"test1","test2","test3"};
            

            【讨论】:

              【解决方案8】:

              你可以:

              var list = new List<string> { "red", "green", "blue" };
              

              List<string> list = new List<string> { "red", "green", "blue" };
              

              结帐:Object and Collection Initializers (C# Programming Guide)

              【讨论】:

                【解决方案9】:

                您可以创建辅助通用静态方法来创建列表:

                internal static class List
                {
                    public static List<T> Of<T>(params T[] args)
                    {
                        return new List<T>(args);
                    }
                }
                

                然后用法就很紧凑了:

                List.Of("test1", "test2", "test3")
                

                【讨论】:

                  【解决方案10】:

                  如果你想创建一个带值的类型列表,这里是语法。

                  假设一类学生喜欢

                  public class Student {
                     public int StudentID { get; set; }
                     public string StudentName { get; set; }
                   }   
                  

                  你可以这样列出:

                  IList<Student> studentList = new List<Student>() { 
                                  new Student(){ StudentID=1, StudentName="Bill"},
                                  new Student(){ StudentID=2, StudentName="Steve"},
                                  new Student(){ StudentID=3, StudentName="Ram"},
                                  new Student(){ StudentID=1, StudentName="Moin"}
                              };
                  

                  【讨论】:

                  • 你甚至不需要new List&lt;Student&gt;new Student之后的()s。
                  【解决方案11】:

                  如果我们假设有一个名为 Country 的类,那么我们可以这样做:

                  var countries = new List<Country>
                  {
                      new Country { Id=1, Name="Germany", Code="De" },
                      new Country { Id=2, Name="France",  Code="Fr" }
                  };
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-08-06
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-03-29
                    • 1970-01-01
                    相关资源
                    最近更新 更多