• <tfoot id='Noa1H'></tfoot>
    <legend id='Noa1H'><style id='Noa1H'><dir id='Noa1H'><q id='Noa1H'></q></dir></style></legend>
  • <small id='Noa1H'></small><noframes id='Noa1H'>

          <bdo id='Noa1H'></bdo><ul id='Noa1H'></ul>
      1. <i id='Noa1H'><tr id='Noa1H'><dt id='Noa1H'><q id='Noa1H'><span id='Noa1H'><b id='Noa1H'><form id='Noa1H'><ins id='Noa1H'></ins><ul id='Noa1H'></ul><sub id='Noa1H'></sub></form><legend id='Noa1H'></legend><bdo id='Noa1H'><pre id='Noa1H'><center id='Noa1H'></center></pre></bdo></b><th id='Noa1H'></th></span></q></dt></tr></i><div id='Noa1H'><tfoot id='Noa1H'></tfoot><dl id='Noa1H'><fieldset id='Noa1H'></fieldset></dl></div>

        如何初始化列表&lt;T&gt;到给定的大小(而不是容量)?

        时间:2023-11-10
        • <small id='apLfM'></small><noframes id='apLfM'>

                <tbody id='apLfM'></tbody>
              <i id='apLfM'><tr id='apLfM'><dt id='apLfM'><q id='apLfM'><span id='apLfM'><b id='apLfM'><form id='apLfM'><ins id='apLfM'></ins><ul id='apLfM'></ul><sub id='apLfM'></sub></form><legend id='apLfM'></legend><bdo id='apLfM'><pre id='apLfM'><center id='apLfM'></center></pre></bdo></b><th id='apLfM'></th></span></q></dt></tr></i><div id='apLfM'><tfoot id='apLfM'></tfoot><dl id='apLfM'><fieldset id='apLfM'></fieldset></dl></div>

              <tfoot id='apLfM'></tfoot>

              <legend id='apLfM'><style id='apLfM'><dir id='apLfM'><q id='apLfM'></q></dir></style></legend>
                <bdo id='apLfM'></bdo><ul id='apLfM'></ul>
                • 本文介绍了如何初始化列表&lt;T&gt;到给定的大小(而不是容量)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  .NET 提供了一个通用列表容器,其性能几乎相同(请参阅数组与列表的性能问题).但是,它们在初始化方面完全不同.

                  .NET offers a generic list container whose performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization.

                  数组很容易用默认值初始化,并且根据定义它们已经具有一定的大小:

                  Arrays are very easy to initialize with a default value, and by definition they already have certain size:

                  string[] Ar = new string[10];
                  

                  这允许人们安全地分配随机项目,例如:

                  Which allows one to safely assign random items, say:

                  Ar[5]="hello";
                  

                  使用列表的事情更加棘手.我可以看到两种进行相同初始化的方法,这两种方法都不是你所说的优雅:

                  with list things are more tricky. I can see two ways of doing the same initialization, neither of which is what you would call elegant:

                  List<string> L = new List<string>(10);
                  for (int i=0;i<10;i++) L.Add(null);
                  

                  string[] Ar = new string[10];
                  List<string> L = new List<string>(Ar);
                  

                  什么是更清洁的方法?

                  到目前为止的答案是指容量,这与预先填充列表不同.例如,在刚刚创建的容量为 10 的列表上,不能执行 L[2]="somevalue"

                  The answers so far refer to capacity, which is something else than pre-populating a list. For example, on a list just created with a capacity of 10, one cannot do L[2]="somevalue"

                  编辑 2:人们想知道我为什么要以这种方式使用列表,因为这不是它们的预期使用方式.我可以看到两个原因:

                  EDIT 2: People wonder why I want to use lists this way, as it is not the way they are intended to be used. I can see two reasons:

                  1. 人们可以非常有说服力地争辩说,列表是下一代"数组,增加了灵活性,几乎没有任何损失.因此,应该默认使用它们.我指出它们可能不那么容易初始化.

                  1. One could quite convincingly argue that lists are the "next generation" arrays, adding flexibility with almost no penalty. Therefore one should use them by default. I'm pointing out they might not be as easy to initialize.

                  我目前正在编写的是一个基类,它提供默认功能作为更大框架的一部分.在我提供的默认功能中,列表的大小是预先知道的,因此我可以使用数组.但是,我想为任何基类提供动态扩展它的机会,因此我选择了一个列表.

                  What I'm currently writing is a base class offering default functionality as part of a bigger framework. In the default functionality I offer, the size of the List is known in advanced and therefore I could have used an array. However, I want to offer any base class the chance to dynamically extend it and therefore I opt for a list.

                  推荐答案

                  我不能说我经常需要这个 - 你能详细说明你为什么需要这个吗?我可能会把它作为一个静态方法放在一个辅助类中:

                  I can't say I need this very often - could you give more details as to why you want this? I'd probably put it as a static method in a helper class:

                  public static class Lists
                  {
                      public static List<T> RepeatedDefault<T>(int count)
                      {
                          return Repeated(default(T), count);
                      }
                  
                      public static List<T> Repeated<T>(T value, int count)
                      {
                          List<T> ret = new List<T>(count);
                          ret.AddRange(Enumerable.Repeat(value, count));
                          return ret;
                      }
                  }
                  

                  可以使用 Enumerable.Repeat(default(T), count).ToList() 但由于缓冲区大小调整,这样做效率低.

                  You could use Enumerable.Repeat(default(T), count).ToList() but that would be inefficient due to buffer resizing.

                  请注意,如果 T 是引用类型,它将存储 count 个为 value 参数传递的引用的副本 - 因此它们将都指同一个对象.这可能是也可能不是您想要的,具体取决于您的用例.

                  Note that if T is a reference type, it will store count copies of the reference passed for the value parameter - so they will all refer to the same object. That may or may not be what you want, depending on your use case.

                  如评论中所述,如果您愿意,可以使 Repeated 使用循环来填充列表.那也会稍微快一点.我个人发现使用 Repeat 的代码更具描述性,并怀疑在现实世界中性能差异无关紧要,但您的里程可能会有所不同.

                  As noted in comments, you could make Repeated use a loop to populate the list if you wanted to. That would be slightly faster too. Personally I find the code using Repeat more descriptive, and suspect that in the real world the performance difference would be irrelevant, but your mileage may vary.

                  这篇关于如何初始化列表&lt;T&gt;到给定的大小(而不是容量)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:初始化 C# 自动属性 下一篇:如何初始化变量?

                  相关文章

                • <i id='A3hdn'><tr id='A3hdn'><dt id='A3hdn'><q id='A3hdn'><span id='A3hdn'><b id='A3hdn'><form id='A3hdn'><ins id='A3hdn'></ins><ul id='A3hdn'></ul><sub id='A3hdn'></sub></form><legend id='A3hdn'></legend><bdo id='A3hdn'><pre id='A3hdn'><center id='A3hdn'></center></pre></bdo></b><th id='A3hdn'></th></span></q></dt></tr></i><div id='A3hdn'><tfoot id='A3hdn'></tfoot><dl id='A3hdn'><fieldset id='A3hdn'></fieldset></dl></div>

                      <small id='A3hdn'></small><noframes id='A3hdn'>

                      <legend id='A3hdn'><style id='A3hdn'><dir id='A3hdn'><q id='A3hdn'></q></dir></style></legend>
                        <bdo id='A3hdn'></bdo><ul id='A3hdn'></ul>
                      <tfoot id='A3hdn'></tfoot>