• 使用Indexer(索引器)小结


    索引器允许按照与数组相同的方式对类、结构或接口进行索引。
    要声明类或结构上的索引器,要使用 this 关键字,如下例所示:
    public int this[int index]    // Indexer declaration
    {
        
    // get and set accessors
    }



    Demo 1
    下面的Demo说明如何声明私有数组字段、arr 和索引器。使用索引器可直接访问实例 test[i]。另一种使用索引器的方法是将数组声明为 public 成员并直接访问它的成员 arr[i]。
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace JackyGao
    {
        
    class IndexerClass
        
    {
            
    private int[] arr = new int[100];
            
    public int this[int index]   // Indexer declaration
            {
                
    get
                
    {
                    
    // Check the index limits.
                    if (index < 0 || index >= 100)
                    
    {
                        
    return 0;
                    }

                    
    else
                    
    {
                        
    return arr[index];
                    }

                }

                
    set
                
    {
                    
    if (!(index < 0 || index >= 100))
                    
    {
                        arr[index] 
    = value;
                    }

                }

            }

        }


        
    class Program
        
    {
            
    static void Main(string[] args)
            
    {
                IndexerClass test 
    = new IndexerClass();
                
    // Call the indexer to initialize the elements #3 and #5.
                test[3= 256;
                test[
    5= 1024;
                
    for (int i = 0; i <= 10; i++)
                
    {
                    System.Console.WriteLine(
    "Element #{0} = {1}", i, test[i]);
                }

            }

        }

    }

    输出:
    Element #0 = 0
    Element #1 = 0
    Element #2 = 0
    Element #3 = 256
    Element #4 = 0
    Element #5 = 1024
    Element #6 = 0
    Element #7 = 0
    Element #8 = 0
    Element #9 = 0
    Element #10 = 0

    PS:当计算索引器的访问时(例如,在 Console.Write 语句中),将调用 get 访问器。因此,如果 get 访问器不存在,将发生编译时错误。


    使用其他值进行索引
    C# 并不将索引类型限制为整数。例如,对索引器使用字符串可能是有用的。通过搜索集合内的字符串并返回相应的值,可以实现此类的索引器。由于访问器可被重载,字符串和整数版本可以共存。
    Demo 2
    在此例中,声明了存储星期几的类。声明了一个 get 访问器,它接受字符串(天名称),并返回相应的整数。例如,星期日将返回 0,星期一将返回 1,等等。
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace JackyGao
    {
        
    // Using a string as an indexer value
        class DayCollection
        
    {
            
    string[] days = "Sun""Mon""Tues""Wed""Thurs""Fri""Sat" };

            
    // This method finds the day or returns -1
            private int GetDay(string testDay)
            
    {
                
    int i = 0;
                
    foreach (string day in days)
                
    {
                    
    if (day == testDay)
                    
    {
                        
    return i;
                    }

                    i
    ++;
                }

                
    return -1;
            }


            
    // The get accessor returns an integer for a given string
            public int this[string day]
            
    {
                
    get
                
    {
                    
    return (GetDay(day));
                }

            }

        }


        
    class Program
        
    {
            
    static void Main(string[] args)
            
    {
                DayCollection week 
    = new DayCollection();
                System.Console.WriteLine(week[
    "Fri"]);
                System.Console.WriteLine(week[
    "Made-up Day"]);
            }

        }

    }

    输出:
    5
    -1


    提高索引器的安全性可靠性有两种主要的方法:
    1、当设置并检索来自索引器访问的任何缓冲区或数组的值时,请始终确保您的代码执行范围和类型检查。
    2、应当为 get 和 set 访问器的可访问性设置尽可能多的限制。这一点对 set 访问器尤为重要。
  • 相关阅读:
    电力基本知识
    .net图表工具汇总
    最重要的十年做什么才不浪费?
    花10分钟看一看少走30年弯路
    给明年依然年轻的我们
    Qt经典—线程、事件与Qobject
    C#源码500份
    .NET 性能优化方法总结==转
    qt +ChartDirector 绘制图表
    创业者,你为什么这么着急?
  • 原文地址:https://www.cnblogs.com/gao/p/637129.html
Copyright © 2020-2023  润新知