<bdo id='hNJBp'></bdo><ul id='hNJBp'></ul>
    1. <tfoot id='hNJBp'></tfoot>

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

      1. <legend id='hNJBp'><style id='hNJBp'><dir id='hNJBp'><q id='hNJBp'></q></dir></style></legend>
      2. <i id='hNJBp'><tr id='hNJBp'><dt id='hNJBp'><q id='hNJBp'><span id='hNJBp'><b id='hNJBp'><form id='hNJBp'><ins id='hNJBp'></ins><ul id='hNJBp'></ul><sub id='hNJBp'></sub></form><legend id='hNJBp'></legend><bdo id='hNJBp'><pre id='hNJBp'><center id='hNJBp'></center></pre></bdo></b><th id='hNJBp'></th></span></q></dt></tr></i><div id='hNJBp'><tfoot id='hNJBp'></tfoot><dl id='hNJBp'><fieldset id='hNJBp'></fieldset></dl></div>
      3. 为什么我可以像 C# 中的数组一样初始化 List?

        Why can I initialize a List like an array in C#?(为什么我可以像 C# 中的数组一样初始化 List?)

          <small id='2IUyP'></small><noframes id='2IUyP'>

            <tfoot id='2IUyP'></tfoot>
            • <bdo id='2IUyP'></bdo><ul id='2IUyP'></ul>

                <tbody id='2IUyP'></tbody>

                  <i id='2IUyP'><tr id='2IUyP'><dt id='2IUyP'><q id='2IUyP'><span id='2IUyP'><b id='2IUyP'><form id='2IUyP'><ins id='2IUyP'></ins><ul id='2IUyP'></ul><sub id='2IUyP'></sub></form><legend id='2IUyP'></legend><bdo id='2IUyP'><pre id='2IUyP'><center id='2IUyP'></center></pre></bdo></b><th id='2IUyP'></th></span></q></dt></tr></i><div id='2IUyP'><tfoot id='2IUyP'></tfoot><dl id='2IUyP'><fieldset id='2IUyP'></fieldset></dl></div>
                  <legend id='2IUyP'><style id='2IUyP'><dir id='2IUyP'><q id='2IUyP'></q></dir></style></legend>
                  本文介绍了为什么我可以像 C# 中的数组一样初始化 List?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!


                  今天我惊讶地发现在 C# 中我可以做到:

                  Today I was surprised to find that in C# I can do:

                  List<int> a = new List<int> { 1, 2, 3 };


                  Why can I do this? What constructor is called? How can I do this with my own classes? I know that this is the way to initialize arrays but arrays are language items and Lists are simple objects ...


                  这是 .NET 中集合初始化语法的一部分.您可以在您创建的任何集合上使用此语法,只要:

                  This is part of the collection initializer syntax in .NET. You can use this syntax on any collection you create as long as:

                  • 它实现了IEnumerable(最好是IEnumerable)


                  发生的情况是调用默认构造函数,然后为初始化程序的每个成员调用 Add(...).

                  What happens is the default constructor is called, and then Add(...) is called for each member of the initializer.


                  Thus, these two blocks are roughly identical:

                  List<int> a = new List<int> { 1, 2, 3 };

                  List<int> temp = new List<int>();
                  List<int> a = temp;

                  如果需要,您可以调用备用构造函数,例如防止在增长过程中 List<T> 过大等:

                  You can call an alternate constructor if you want, for example to prevent over-sizing the List<T> during growing, etc:

                  // Notice, calls the List constructor that takes an int arg
                  // for initial capacity, then Add()'s three items.
                  List<int> a = new List<int>(3) { 1, 2, 3, }

                  请注意,Add() 方法不必采用单个项目,例如 DictionaryAdd() 方法code> 需要两个项目:

                  Note that the Add() method need not take a single item, for example the Add() method for Dictionary<TKey, TValue> takes two items:

                  var grades = new Dictionary<string, int>
                          { "Suzy", 100 },
                          { "David", 98 },
                          { "Karen", 73 }


                  var temp = new Dictionary<string, int>();
                  temp.Add("Suzy", 100);
                  temp.Add("David", 98);
                  temp.Add("Karen", 73);
                  var grades = temp;

                  因此,要将它添加到您自己的类中,您需要做的就是实现 IEnumerable(同样,最好是 IEnumerable)并创建一个或更多 Add() 方法:

                  So, to add this to your own class, all you need do, as mentioned, is implement IEnumerable (again, preferably IEnumerable<T>) and create one or more Add() methods:

                  public class SomeCollection<T> : IEnumerable<T>
                      // implement Add() methods appropriate for your collection
                      public void Add(T item)
                          // your add logic    
                      // implement your enumerators for IEnumerable<T> (and IEnumerable)
                      public IEnumerator<T> GetEnumerator()
                          // your implementation
                      IEnumerator IEnumerable.GetEnumerator()
                          return GetEnumerator();

                  然后你可以像 BCL 集合一样使用它:

                  Then you can use it just like the BCL collections do:

                  public class MyProgram
                      private SomeCollection<int> _myCollection = new SomeCollection<int> { 13, 5, 7 };    
                      // ...

                  (有关详细信息,请参阅 MSDN)

                  (For more information, see the MSDN)

                  这篇关于为什么我可以像 C# 中的数组一样初始化 List?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                  <legend id='Ewegt'><style id='Ewegt'><dir id='Ewegt'><q id='Ewegt'></q></dir></style></legend>
                        <tbody id='Ewegt'></tbody>

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

                          <bdo id='Ewegt'></bdo><ul id='Ewegt'></ul>

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

                          <tfoot id='Ewegt'></tfoot>