<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 泛型代碼中的默認關鍵字(C# 編程指南) 在泛型類和泛型方法中產生的一個問題是,在預先未知以下情況時,如何將默認值分配給參數化類型 T: * T 是引用類型還是值類型。 * 如果 T 為值類型,則它是數值還是結構。 給定參數化類型 T 的一個變量 t,只有當 T 為引用類型時,語句 t = null 才有效;只有當 T 為數值類型而不是結構時,語句 t = 0 才能正常使用。解決方案是使用 **default** 關鍵字,此關鍵字對于引用類型會返回 null,對于數值類型會返回零。對于結構,此關鍵字將返回初始化為零或 null 的每個結構成員,具體取決于這些結構是值類型還是引用類型。對于可以為 null 的值類型,默認返回 [System.Nullable&lt;T&gt;](https://msdn.microsoft.com/zh-cn/library/b3h38hb0.aspx),它像任何結構一樣初始化。 以下來自 GenericList&lt;T&gt; 類的示例顯示了如何使用 **default** 關鍵字。有關更多信息,請參見[泛型概述](https://msdn.microsoft.com/zh-cn/library/0x6a29h6.aspx)。 ``` namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // Test with a non-empty list of integers. GenericList<int> gll = new GenericList<int>(); gll.AddNode(5); gll.AddNode(4); gll.AddNode(3); int intVal = gll.GetLast(); // The following line displays 5. System.Console.WriteLine(intVal); // Test with an empty list of integers. GenericList<int> gll2 = new GenericList<int>(); intVal = gll2.GetLast(); // The following line displays 0. System.Console.WriteLine(intVal); // Test with a non-empty list of strings. GenericList<string> gll3 = new GenericList<string>(); gll3.AddNode("five"); gll3.AddNode("four"); string sVal = gll3.GetLast(); // The following line displays five. System.Console.WriteLine(sVal); // Test with an empty list of strings. GenericList<string> gll4 = new GenericList<string>(); sVal = gll4.GetLast(); // The following line displays a blank line. System.Console.WriteLine(sVal); } } // T is the type of data stored in a particular instance of GenericList. public class GenericList<T> { private class Node { // Each node has a reference to the next node in the list. public Node Next; // Each node holds a value of type T. public T Data; } // The list is initially empty. private Node head = null; // Add a node at the beginning of the list with t as its data value. public void AddNode(T t) { Node newNode = new Node(); newNode.Next = head; newNode.Data = t; head = newNode; } // The following method returns the data value stored in the last node in // the list. If the list is empty, the default value for type T is // returned. public T GetLast() { // The value of temp is returned as the value of the method. // The following declaration initializes temp to the appropriate // default value for type T. The default value is returned if the // list is empty. T temp = default(T); Node current = head; while (current != null) { temp = current.Data; current = current.Next; } return temp; } } } ``` ## 請參閱 [System.Collections.Generic](https://msdn.microsoft.com/zh-cn/library/system.collections.generic.aspx) [C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx) [泛型(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/512aeb7t.aspx) [泛型方法(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/twcad0zb.aspx) [.NET Framework 中的泛型](https://msdn.microsoft.com/zh-cn/library/ms172192.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看