黑马程序员之C#学习笔记:C#索引器与属性的区别、索引器的具体使用

--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------

C#中索引器和属性类似,但也有一些区别:


a、    属性可以调用方法就如同他们是公共数据成员,而索引器可以调用对象上的方法,如同对象是一个数组。

b、    属性可以通过简单的名称访问, 而索引器需要通过索引器访问。

c、    属性可以为静态成员或者实例成员,而索引器必须是实例成员。

d、    属性的get访问器没有参数, 而索引器上的get访问器具有与索引器相同的形参表。

e、    属性的set访问器包含隐式value参数,而对于索引器,除了value参数外,索引器的set访问器还具有与索引器相同的形参表。

        索引器使得对象可按照与数组相似的方法进行索引,索引器可被重载,当访问二维数组时可以有多个形参;索引器不必根据整数值进行索引,由你决定如何定义特定的查找机制;要声明类或结构上的索引器,请使用this关键字。
using System;
using System.Collections.Generic;
using System.Text;
namespace Index
{
         class sample //这个类告诉我们如何使用客户端代码索引器
         {
                  private T[] arr = new T[100];
                  public T this[int i] //索引器的签名由其形参的数量和类型组成。
                  {
                               get { return arr;}
                               set { arr = value;}
                  }
          }
          class IndexerClass
           {
                      private int[] arr = new int[100]; //定义数组
                      public int this[int index] //索引器声明
                      {
                            get
                            {
                                        if (index < 0 || index >100)
                                                    return 0;
                                        return arr[index];
                           }
                           set
                           {
                                      if (!(index < 0 || index > 100)) arr[index] = value;
                           }
                    }
           }
           class String_index
           {
                         string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
                         private int GetDay(string testday)
                         {
                                    int i = 0;
                                    foreach(string day in days)
                                    {
                                             if (day == testday)
                                                       return i;
                                               i++;
                                      }
                                      return -1;
                         }
                         public int this[string day]
                         {
                                      get
                                      {
                                                return GetDay(day);
                                      }
                          }
            }
            class Program
            {
                        static void Main(string[] args)
                        {
                                   sample string_value = new sample();
                                   string_value[0] = "Hello world";
                                   System.Console.WriteLine(string_value[0]);

                                   indexerClass test = new IndexerClass();

                                   //调用索引器初始化第2、4个数据

                                   test[3] = 123;

                                    test[5] = 1024;

                                    for (int i = 0; i <= 10; i++ )

                                    {

                                             System.Console.WriteLine("数据为#{0} = {1}", i, test);

                                    }

                                   String_index week = new String_index();

                                    System.Console.WriteLine("这是一周的第{0}天", week["Tues"]);

                                    Console.ReadKey();

                           }

               }

}


--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------


你可能感兴趣的:(黑马程序员C#学习笔记)