鑫诚饰链厂 發表於 2020-9-4 08:48:00

C#-索引器

<h2>概念</h2>
<p>索引器(Indexer) 允许类中的对象可以像数组那样方便、直观的被引用。当为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样。<br>索引器可以有参数列表,且只能作用在实例对象上,而不能在类上直接作用。<br>可以使用数组访问运算符([ ])来访问该类的实例。<br>索引器的行为的声明在某种程度上类似于属性(property)。属性可使用 get 和 set 访问器来定义索引器。但是属性返回或设置的是一个特定的数据成员,而索引器返回或设置对象实例的一个特定值。</p>
<p>定义一个一维数组的索引器:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">element-type this
{
   // get 访问器
   get
   {
      // 返回 index 指定的值
   }

   // set 访问器
   set
   {
      // 设置 index 指定的值
   }
}
</pre>
</div>
<p><strong>提示:索引器必须以this关键字定义,this 是类实例化之后的对象</strong></p>
<p>实例:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">using System;

namespace C_Pro
{
    public class Student
    {
      private string name;
      private string grade;

      public string Name
      {
            get {return name; }
            set {name = value; }
      }

         public string Grade
      {
            get {return grade; }
            set {grade = value; }
      }

      // 定义索引器
      public string this
      {
            get
            {
                if (index == 0) return name;
                else if (index == 1) return grade;
                else return null;
            }
            set
            {
                if (index == 0) name = value;
                else if (index == 1) grade = value;
            }
      }
      static void Main(string[] args)
      {
            Student s = new Student();
            s = "Jeson";
            s = "First-year";

            Console.WriteLine(s.Name);
            Console.WriteLine(s.Grade);
            Console.ReadKey();
      }
    }
}
</pre>
</div>
<p>运行后结果:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">Jeson

First-year</pre>
</div>
<h2>重载索引器</h2>
<p>索引器(Indexer)可被重载。索引器声明的时候也可带有多个参数,且每个参数可以是不同的类型。没有必要让索引器必须是整型的。C# 允许索引器可以是其他类型,例如,字符串类型。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">using System;

namespace C_Pro
{
    public class IndexedNames
    {
      private string[] namelist = {"a", "b", "c", "d"};

      // 输入namelist的index返回对应的值
      public string this
      {
            get
            {
                return namelist;
            }
            set
            {
                namelist = value;
            }
      }

      // 输入namelist的值,返回对应的索引
         public int this
      {
            get
            {
                for (int i=0; i&lt;namelist.Length; i++)
                {
                  if (namelist == name) return i;
                }
               
                return -1;
            }

      }

      static void Main(string[] args)
      {

            IndexedNames name = new IndexedNames();
            
            Console.WriteLine(name);
            Console.WriteLine(name["a"]);

      }
    }
}
</pre>
</div>
<p>运行后结果:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">b
0
</pre>
</div>
<h2>索引器与数组的区别:</h2>
<ul>
<li>索引器的索引值(Index)类型不限定为整数,用来访问数组的索引值(Index)一定为整数,而索引器的索引值类型可以定义为其他类型。</li>
<li>索引器允许重载, 一个类不限定为只能定义一个索引器,只要索引器的函数签名不同,就可以定义多个索引器,可以重载它的功能。</li>
<li>索引器不是一个变量,索引器没有直接定义数据存储的地方,而数组有。索引器具有Get和Set访问器。</li>
</ul>
<h2>索引器与属性的区别:</h2>
<ul>
<li>索引器以函数签名方式 this 来标识,而属性采用名称来标识,名称可以任意。</li>
<li>索引器可以重载,而属性不能重载。</li>
<li>索引器不能用static 来进行声明,而属性可以。索引器永远属于实例成员,因此不能声明为static。</li>
</ul>
<p> </p><br><br>
来源:https://www.cnblogs.com/tynam/p/13611709.html
頁: [1]
查看完整版本: C#-索引器