C# 索引器

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

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

来访问该类的实例。

以为所引起语法如下:

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>
                     



时间: 05-04

C# 索引器的相关文章

C#之索引器

实际中不使用这个东西,只做了解 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 索引器 { class Program { static void Main(string[] args) { person p = new person(); p[0] = 1; p[1] = 2; p[2] =

索引器的使用

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 索引器的使用 { class Program { static void Main(string[] args) { //int[] nums = { 1, 2, 3, 4, 5 }; //Console.WriteLine(nums[2]); P

索引器(C# 编程指南)

索引器(C# 编程指南) Visual Studio 2015 其他版本 索引器允许类或结构的实例就像数组一样进行索引. 索引器类似于属性,不同之处在于它们的取值函数采用参数. 在下面的示例中,定义了一个泛型类,并为其提供了简单的 get 和 set 取值函数方法(作为分配和检索值的方法). Program 类创建了此类的一个实例,用于存储字符串. C# class SampleCollection<T> { // Declare an array to store the data elem

18._4索引器概述及声明

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _18._4索引器概述及声明 { public class Clerk { private string name; public string Name { get { return name; } set { name = value; } }

索引器实现

索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 (3)索引器不是一个变量 索引器和属性的不同点 (1)属性以名称来标识,索引器以函数形式标识 (2)索引器可以被重载,属性不可以 (3)索引器不能声明为static,属性可以 代码实现: 1 //普通索引器 2 public class SimpleClass 3 { 4 string[] arr

《Inside C#》笔记(六) 属性、数组、索引器

一 属性 a) 属性可用于隐藏类的内部成员,对外提供可控的存取接口.属性相当于有些语言的getter.setter方法,只是使用起来更加方便一点,而且查看对应的IL码可以看到,属性的本质也确实是方法. b) 通过只提供get,可以让属性只读.只写属性也可以,但没有用过. c) 属性除了用来控制对类成员的访问外,还可以在get或set的时候通过编码进行一些附加的动作. d) 属性也可以被继承.重写. 二 数组 a) 在C#中,所有数组都继承自System.Array类.数组也是对象,所以声明的数组

C#索引器:在集合或数组中取出某一个元素 举例 _【转】

Garmmar: [访问修饰符] 数据类型 this[参数列表] { get { 获取索引器的内容 } set { 设置索引器的内容 } } Eg: 1 <span style="font-size:14px;">using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace IndexerUsing 6 { 7 class Photo 8 { 9 10 private

接口、索引器、Foreach的本质(学习笔记)

接口 什么是接口? 接口代表一种能力,和抽象类类似但比抽象类的抽象程度更高! 接口的定义: 1 public interface IEat//定义一个接口 2 { 3 void Eat(string food);//为该接口定义一种能力 4 } 接口的定义 从上边的例子中我们可以看到,接口中的方法是没有方法体的甚至连访问修饰符都没有.而且在接口中只能有方法.属性.索引器及事件! 接口的使用: 1 public class Dog:IEat //Dog类实现IEat接口 2 { 3 //Dog类实

public animal this[int index]|索引器的使用

学习如何使用索引器,索引器的使用是public 类型 this[int index]{get{};set{}} ,访问通过类的实例(对象)加[i], 例如animal[i],就像访问数组一样,其实就是类的数组访问的使用书写. 使用详情请看msdn. 例子如下: class IndexerClass { private int[] arr = new int[100]; public int this[int index] // Indexer declaration { get { // Che