码迷,mamicode.com
首页 > Windows程序 > 详细

C# 索引器

时间:2015-05-06 23:08:44      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:

索引器允许累或结构的实例像数组一样进行索引。

当你定义一个索引器时,该类的行为就会像一个虚拟数组,你可以使用数组访问运算符([])

来访问该类的实例。

以为所引起语法如下:

element-type this[int index] 
{
   // get 访问器
   get 
   {
      // 返回 index 指定的值
   }

   // set 访问器
   set 
   {
      // 设置 index 指定的值 
   }
}                                                                                                               
//示例代码如下:   
class SampleCollection<T>
{
    private T[] arr = new T[100];
    public T this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        SampleCollection<string> stringCollection = new SampleCollection<string>();
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}
<ul style="font: 14px/21px 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; margin-bottom: 14px; word-spacing: 0px; white-space: normal; widows: 1; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;"><li style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; list-style-image: none;"><p style="color: rgb(102, 102, 102); line-height: 21px; padding-bottom: 0px; font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; font-size: 14px; margin-top: 0px; margin-bottom: 14px;"><span class="sentence" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xml:space="preserve" data-source="Indexers enable objects to be indexed in a similar manner to arrays." data-guid="2fdab59da0ce5d6b2b8701672681c8cb"><sentencetext style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml">使用索引器可以用类似于数组的方式为对象建立索引。</sentencetext></span></p></li><li style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; list-style-image: none;"><p style="color: rgb(102, 102, 102); line-height: 21px; padding-bottom: 0px; font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; font-size: 14px; margin-top: 0px; margin-bottom: 14px;"><span class="sentence" id="mt7" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xml:space="preserve" data-source="A <span><span class='input'>get</span></span> accessor returns a value." data-guid="5e97def07fe15e931d526cafa5b97644"><sentencetext style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml"><span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml"><span class="input" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; font-weight: 700;">get</span><span> </span></span>访问器返回值。</sentencetext></span><span> </span><span class="sentence" id="mt8" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xml:space="preserve" data-source="A <span><span class='input'>set</span></span> accessor assigns a value." data-guid="66b9797a805b510e697d60c805140aeb"><sentencetext style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml"><span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml"><span class="input" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; font-weight: 700;">set</span><span> </span></span>访问器分配值。</sentencetext></span></p></li><li style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; list-style-image: none;"><p style="color: rgb(102, 102, 102); line-height: 21px; padding-bottom: 0px; font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; font-size: 14px; margin-top: 0px; margin-bottom: 14px;"><span class="sentence" id="mt9" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xml:space="preserve" data-source="The <span class='mtpsTagOuterHtml'><span>this</span></span> keyword is used to define the indexers." data-guid="c34f23e68daf270013978542591de397"><sentencetext style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml"><a target=_blank style="color: rgb(19, 130, 206); font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; text-decoration: none;" href="https://msdn.microsoft.com/zh-cn/library/vstudio/dk1507sz(v=vs.110).aspx">this</a><span> </span>关键字用于定义索引器。</sentencetext></span></p></li><li style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; list-style-image: none;"><p style="color: rgb(102, 102, 102); line-height: 21px; padding-bottom: 0px; font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; font-size: 14px; margin-top: 0px; margin-bottom: 14px;"><span class="sentence" id="mt10" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xml:space="preserve" data-source="The <span class='mtpsTagOuterHtml'><span>value</span></span> keyword is used to define the value being assigned by the <span><span class='input'>set</span></span> indexer." data-guid="599578d2a780928307c04f6ca2b0f058"><sentencetext style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml"><a target=_blank style="color: rgb(19, 130, 206); font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; text-decoration: none;" href="https://msdn.microsoft.com/zh-cn/library/vstudio/a1khb4f8(v=vs.110).aspx">value</a><span> </span>关键字用于定义由<span> </span><span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml"><span class="input" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; font-weight: 700;">set</span></span><span> </span>索引器分配的值。</sentencetext></span></p></li><li style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; list-style-image: none;"><p style="color: rgb(102, 102, 102); line-height: 21px; padding-bottom: 0px; font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; font-size: 14px; margin-top: 0px; margin-bottom: 14px;"><span class="sentence" id="mt11" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xml:space="preserve" data-source="Indexers do not have to be indexed by an integer value; it is up to you how to define the specific look-up mechanism." data-guid="96e6f51005a2a6497ead4c6dc2b40eff"><sentencetext style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml">索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。</sentencetext></span></p></li><li style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; list-style-image: none;"><p style="color: rgb(102, 102, 102); line-height: 21px; padding-bottom: 0px; font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; font-size: 14px; margin-top: 0px; margin-bottom: 14px;"><span class="sentence" id="mt12" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xml:space="preserve" data-source="Indexers can be overloaded." data-guid="7c478030aaff893e66bdb99b3fa49e0b"><sentencetext style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml">索引器可被重载。</sentencetext></span></p></li><li style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; list-style-image: none;"><p style="color: rgb(102, 102, 102); line-height: 21px; padding-bottom: 0px; font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; font-size: 14px; margin-top: 0px; margin-bottom: 14px;"><span class="sentence" id="mt13" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xml:space="preserve" data-source="Indexers can have more than one formal parameter, for example, when accessing a two-dimensional array." data-guid="3cd0ab549a72d474e4fe90e300e6a66e"><sentencetext style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;" xmlns="http://www.w3.org/1999/xhtml">索引器可以有多个形参,例如当访问二维数组时。</sentencetext></span></p></li></ul>

                     


C# 索引器

标签:

原文地址:http://blog.csdn.net/ilipan/article/details/45538505

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!