zoukankan      html  css  js  c++  java
  • .Net学习难点讨论系列16 索引器

    索引器是一个我们经常打交道的特性,在编程过程中,多多少少都会用到索引器。而关于索引器一些高级话题,如给自定义的类添加索引器等也是本文着重介绍的。索引器本质上是一组getset访问器, []中提供的是get访问器查找元素所要的参数,以及查找set访问器所要设置的元素时使用的参数。一个类或结构中只能有一个索引器且名称只能为this(但是索引器可以重载,重载方法就是提供不同类型的索引参数,这也是特别值得注意的是一点,索引器可以有多个索引参数,即下面语法结构的[]中可以提供多个参数(当然这样使用索引器时也要提供相应数量的参数))。

    索引器的语法结构

     1 public type this[type index]
     2 {
     3     get
     4     {
     5         GetAccessor
     6     }
     7     set
     8     {
     9         SetAccessor
    10     }
    11 }

     

    第一个type是索引器(返回值)的类型,正如定义属性中的类型。而第二个type是索引的类型。类似于属性,定义的索引器也不会被分配命名空间,且get,set可以只实现一个或者都实现,但属性通常用于访问单个数据成员,索引用于访问多个数据成员。

    在索引器中应该对索引器进行范围检查防止出现异常。另外不可以声明static的索引器。示例:下列自定义类型提供了一个索引器以使我们用更方便的方法来操作整型中的位。

     1 struct IntBits
     2 {
     3     public IntBits(int initialBitValue)
     4     {
     5         bits = initialBitValue;
     6     }
     7 
     8     private int bits;
     9 
    10     public bool this[int index]
    11     {
    12         get
    13         {
    14             return (bits & (1 << index)) != 0;
    15         }
    16         set
    17         {
    18             //如果value为true,就开启比特,否则把它关闭
    19             if (value)
    20                 bits |= (1 << index);
    21             else
    22                 bits &= -(1 << index);
    23         }
    24     }
    25 }

     

    这个类的使用方式:

    1 int adapted = 63;
    2 IntBits bits = new IntBits(adapted);
    3 //获取索引位置6的bool值
    4 bool peek = bits[6];
    5 //将索引0的比特设为true
    6 bits[0] = true;
    7 //将索引31的比特设为false
    8 bits[31] = false;

     

    索引器的下标可以是字符串等非整数。索引器不可以作为ref或者out参数。

    下面再提供另一个索引器定义的示例:

     1 class Employee
     2 {
     3     public string LastName;
     4     public string FirstName;
     5     public string CityOfBirth;
     6 
     7     public string this[int index]
     8     {
     9         set
    10         {
    11             switch (index)
    12             {
    13                 case 0:
    14                     LastName = value;
    15                     break;
    16                 case 1:
    17                     FirstName = value;
    18                     break;
    19                 case 2:
    20                     CityOfBirth = value;
    21                     break;
    22                 default:
    23                     throw new ArgumentOutOfRangeException("index");
    24             }
    25         }
    26         get
    27         {
    28             switch (index)
    29             {
    30                 case 0:
    31                     return LastName;
    32                 case 1:
    33                     return FirstName;
    34                 case 2:
    35                     return CityOfBirth;
    36                 default:
    37                     throw new ArgumentOutOfRangeException("index");
    38             }
    39         }
    40     }
    41 }

    下面的代码展示了索引器重载的代码结构:

     1 class MyClass
     2 {
     3     public string this[int index]
     4     {
     5         set { … }
     6         get { … }
     7     }
     8 
     9     public string this[int index1, int index2]
    10     {
    11         set { … }
    12         get { … }
    13     }
    14 
    15     public string this[float index]
    16     {
    17         set { … }
    18         get { … }
    19     }
    20 }

    索引器中访问器的访问修饰符与属性访问器的遵守一致的规则:

    Ø  当属性或索引既有get访问器也有set访问器时才能给访问器添加访问修饰符。

    Ø  只能给两个访问器中的一个添加访问修饰符。

    Ø  访问器的修饰符需要比成员的访问级别有更严格的限制。

     

    另外,像是HashTable等字典类内部使用了索引器,从而可以使下列代码1用代码2这种更易懂的方式来表示:

    方式1

    1 Hashtable ages = new Hashtable();
    2 ages.Add("John", 23);

    方式2

    1 ages["Green"] = 36;

     

     接口中的索引器

    类似于接口中定义的属性的方式,在索引器中也定义索引器的getset访问器也不写实现而是直接给出一个分号。

    1 interface IRawInt
    2 {
    3     bool this[int index] { get; set; }
    4 }

    接口的实现分隐式与显式,隐式实现时,可以标记public等,也可以标记virtual已被其子类重写。如果是显式实现则不能标记publicvirtual等。

    索引器中还结合泛型的使用,如Dictionary<T,U>中索引器的实现形如:

    1 public virtual U this[T key]{ get; set; }


     

     

     

     

  • 相关阅读:
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (四) 自动化部署
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (三) 服务观测
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (二) 部署微服务程序
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (一) 部署 Nacos
    C++知识点
    libmkl 学习笔记
    基于tesseract-OCR进行中文识别
    poco编译与运行
    Linux下的I/O复用与epoll详解(转载)
    高并发网络编程之epoll详解(转载)
  • 原文地址:https://www.cnblogs.com/lsxqw2004/p/2063912.html
Copyright © 2011-2022 走看看