zoukankan      html  css  js  c++  java
  • C# 索引器

    一、索引器,类似于数组,它可以让你在实例化之后像数组一样通过索引调用对象,但是又和数组不同:

    1、索引器可以重载;

    2、索引器的[index]值类型不受限制;

    3、索引器不是一个变量。

    二、索引器和属性也相像,但是又和属性不同:

    1、属性以名称来标识,索引器以函数形式来标识,即索引器可以有参数列表;

    2、属性不可以重载,索引器可以;

    3、属性可以声明为static,索引器不可以。

    Main方法:

    class Indexer
        {
            static void Main(string[] args)
            {
                //索引器的简单示例
                IndexerClassDemo indexer = new IndexerClassDemo();
           //索引器重载示例 IndexerReloadClassDemo indexerReload = new IndexerReloadClassDemo();
           //索引器多参示例 IndexerParamtersClassDemo indexerParamters = new IndexerParamtersClassDemo(); //索引器赋值 indexer[0] = "Zhangs"; indexer[1] = "Lis"; indexer["Demo001"] = "Demo001"; indexer["Demo002"] = "Demo002"; indexerReload[0] = "Relaod001"; indexerReload[1] = "Relaod002"; indexerReload["Relaod003"] = 3; indexerParamters["Ls", "Xi'an"] = 17; //输出索引器的值,其实就是调用其get方法。 Console.WriteLine(indexer[0]); Console.WriteLine(indexer["Demo002"]); Console.WriteLine(indexerReload["Relaod003"].ToString()); Console.WriteLine(indexerParamters["Ls", "Xi'an"]); Console.ReadKey(); } }

      

    索引器的简单示例:

    /// <summary>
        /// 简单应用实例
        /// </summary>
        public class IndexerClassDemo
        {
            //所引起必须以  this  关键字定义,其实这个  this  就是实例化后的对象
            private string[] name = new string[2];
            public string this[int index]
            {
                //实现所引起的get方法
                get
                {
                    if (index < 3)
                    {
                        return name[index];
                    }
                    return null;
                }
                //实现所引起的set方法
                set
                {
                    name[index] = value;
                }
            }
    
            private Hashtable ht = new Hashtable();        
            public string this[string index]
            {
                get
                {
                    return ht[index].ToString();
                }
                set
                {
                    ht.Add(index, value);
                }
            }
        }
    

    索引器的重载示例:

    /// <summary>
        /// 索引器的重载
        /// </summary>
        public class IndexerReloadClassDemo
        {
            //所引起必须以  this  关键字定义,其实这个  this  就是实例化后的对象
            private Hashtable ht = new Hashtable();
            public string this[int indexReload]
            {
                get
                {
                    return ht[indexReload].ToString();
                }
                set
                {
                    ht.Add(indexReload, value);
                }
            }
    
            public int this[string name]
            {
                get
                {
                    foreach (DictionaryEntry de in ht)
                    {
                        if (de.Value.ToString() == name)
                        {
                            return Convert.ToInt32(de.Key);
                        }
                    }
                    return -1;
                }
                set
                {
                    ht.Add(value, name);
                }
            }
        }
    

    索引器多参示例:

    /// <summary>
        /// 多参索引器
        /// </summary>
        public class IndexerParamtersClassDemo
        {
            private ArrayList al;
            public IndexerParamtersClassDemo()
            {
                al = new ArrayList();
            }
    
            public int this[string name, string addr]
            {
                get
                {
                    foreach (PersonInforMessage info in al)
                    {
                        if (info.Name == name && info.Addr == addr)
                        {
                            return info.Age;
                        }
                    }
                    return -1;
                }
                set
                {
                    al.Add(new PersonInforMessage(name, addr, Convert.ToInt32(value)));
                }
            }
        }
    
        public class PersonInforMessage
        {
            private string name;
            private string addr;
            private int age;
    
            public PersonInforMessage(string name, string addr, int age)
            {
                this.name = name;
                this.addr = addr;
                this.age = age;
            }
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            public string Addr
            {
                get { return addr; }
                set { addr = value; }
            }
            public int Age
            {
                get { return age; }
                set { age = value; }
            }
        }
    }
  • 相关阅读:
    分式函数的变换源
    分式之殇
    两条直线的位置关系
    数列专题思维导图
    数列通项公式思维导图
    函数与导数思维导图
    三角函数思维导图
    函数与初等函数思维导图
    集合思维导图
    npm包发布正式和测试版
  • 原文地址:https://www.cnblogs.com/jack-Leo/p/6408040.html
Copyright © 2011-2022 走看看