<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# 編程指南) 索引器允許類或結構的實例就像數組一樣進行索引。索引器類似于[屬性](https://msdn.microsoft.com/zh-cn/library/x9fsa0sw.aspx),不同之處在于它們的取值函數采用參數。 在下面的示例中,定義了一個泛型類,并為其提供了簡單的 [get](https://msdn.microsoft.com/zh-cn/library/ms228503.aspx) 和 [set](https://msdn.microsoft.com/zh-cn/library/ms228368.aspx) 取值函數方法(作為分配和檢索值的方法)。 Program 類創建了此類的一個實例,用于存儲字符串。 ``` class SampleCollection<T> { // Declare an array to store the data elements. private T[] arr = new T[100]; // Define the indexer, which will allow client code // to use [] notation on the class instance itself. // (See line 2 of code in Main below.) public T this[int i] { get { // This indexer is very simple, and just returns or sets // the corresponding element from the internal array. return arr[i]; } set { arr[i] = value; } } } // This class shows how client code uses the indexer. class Program { static void Main(string[] args) { // Declare an instance of the SampleCollection type. SampleCollection<string> stringCollection = new SampleCollection<string>(); // Use [] notation on the type. stringCollection[0] = "Hello, World"; System.Console.WriteLine(stringCollection[0]); } } // Output: // Hello, World. ``` | ![](https://box.kancloud.cn/2016-01-31_56adb62c1380a.jpg) 注意 | | :-- | | 有關更多示例,請參閱[相關章節](#BKMK_RelatedSections)。 | ## 表達式主體定義 直接只返回表達式結果的索引器很常見。下面的語法快捷方式使用 **=&gt;** 來定義這些索引器: ``` public Customer this[long id] => store.LookupCustomer(id); ``` 索引器必須為只讀,并且你不能使用 **get** 取值函數關鍵字。 ## 索引器概述 * 使用索引器可以用類似于數組的方式為對象建立索引。 * **get** 取值函數返回值。 **set** 取值函數分配值。 * [this](https://msdn.microsoft.com/zh-cn/library/dk1507sz.aspx) 關鍵字用于定義索引器。 * [value](https://msdn.microsoft.com/zh-cn/library/a1khb4f8.aspx) 關鍵字用于定義由 **set** 索引器分配的值。 * 索引器不必根據整數值進行索引;由你決定如何定義特定的查找機制。 * 索引器可被重載。 * 索引器可以有多個形參,例如當訪問二維數組時。 <a id="BKMK_RelatedSections"></a> ## 相關章節 * [使用索引器(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/2549tw02.aspx) * [接口中的索引器(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/tkyhsw31.aspx) * [屬性和索引器之間的比較(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/4bsztef7.aspx) * [限制訪問器可訪問性(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/75e8y5dd.aspx) ## C# 語言規范 有關詳細信息,請參閱 [C# 語言規范](https://msdn.microsoft.com/zh-cn/library/ms228593.aspx)。該語言規范是 C# 語法和用法的權威資料。 ## 請參閱 [C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx) [屬性(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/x9fsa0sw.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>

                              哎呀哎呀视频在线观看