(四十三)C#编程基础复习——C#索引器(Indexer)

索引器是类中的一个特殊成员,它能够让对象以类似数组的形式来操作,使程序看起来更为直观,更容易编写。索引器与属性类似,在定义索引器时同样会用到get和set访问器,不同的是,访问属性不需要提供参数而访问索引器则需要提供相应的参数。

一、定义索引器

C#中属性的定义需要提供属性名称,而索引器则不需要具体名称,而是使用this关键字来定义,语法如下:

索引器类型this[int index]
{
   //get访问器
   get
   {
     //返回index指定的值
    }
    //set访问器
    set
    {
     //设置index指定的值
     }
}

案例如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _004
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Program names = new Program();
            names[0] = "C#中文网";
            names[1] = "http://c.biancheng.net/";
            names[2] = "C#教程";
            names[3] = "索引器";
            for(int i=0;i<Program.size;i++)
            {
                Console.WriteLine(names[i]);
            }
            Console.ReadKey();
        }
        public static int size = 10;
        private string[] namelist = new string[size];
        public Program()
        {
            for(int i=0;i<size;i++)
            {
                namelist[i] = "NULL";
            }
        }
        public string this[int index]
        {
            get
            {
                string tmp;
                if(index>=0 && index<=size-1)
                {
                    tmp = namelist[index];
                }
                else
                {
                    tmp = "";
                }
                return (tmp);
            }
            set
            {
                if(index>=0 && index<=size-1)
                {
                    namelist[index] = value;
                }
            }
        }
    }
}

二、索引器重载

索引器可以被重载,而且在声明索引器时也可以带有多个参数,每个参数可以是不同的类型。另外,索引器中的索引不必时整数,也可以是其他类型,例如字符串类型。

示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _004
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Program names = new Program();
            names[0] = "C#中文网";
            names[1] = "http://c.biancheng.net/";
            names[2] = "C#教程";
            names[3] = "索引器";
            //使用带有int参数的第一个索引器
            for(int i=0;i<Program.size;i++)
            {
                Console.WriteLine(names[i]);
            }
            //使用带有string参数的第二个索引器
            Console.WriteLine(@"C#教程的索引为:{0}", names["C#教程"]); 
            Console.ReadKey();
        }
        public static int size = 10;
        private string[] namelist = new string[size];
        public Program()
        {
            for(int i=0;i<size;i++)
            {
                namelist[i] = "NULL";
            }
        }
        public string this[int index]
        {
            get
            {
                string tmp;
                if(index>=0 && index<=size-1)
                {
                    tmp = namelist[index];
                }
                else
                {
                    tmp = "";
                }
                return (tmp);
            }
            set
            {
                if(index>=0 && index<=size-1)
                {
                    namelist[index] = value;
                }
            }
        }
        public int this[string name]
        {
            get
            {
                int index = 0;
                while(index<size)
                {
                    if (namelist[index]==name)
                    {
                        return index;
                    }
                    index++;
                }
                return index;
            }
        }
    }
}

 

posted @   代号六零一  阅读(88)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示