c#索引器的使用

索引器(Indexer)是C#引入的一个新型的类成员,它使得类中的对象可以像数组那样方便、直观的被引用。索引器类似于属性,但索引器可以有参数列表,且只能作用在实例对象上,而不能在类上直接作用。定义了索引器的类可以让您像访问数组一样的使用 [ ] 运算符访问类的成员。

索引器允许类和结构的实例按照与数组相同的方式进行索引,索引器类似与属性,不同之处在于他们的访问器采用参数。被称为有参属性。

案例:

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

namespace 索引器
{
    class Program
    {
        static void Main(string[] args)
        {
            People p = new People();
            //使用索引器给类的两个属性赋值
            p[0] = "张三";
            p[1] = "李四";
            p[2] = "王五";
            p[3] = "王麻子";
            Console.WriteLine("p[0]="+p[0]);
            Console.WriteLine("p[1]=" + p[1]);
            Console.WriteLine("p[2]=" + p[2]);
            Console.ReadLine();
        }
        public class People
        {
            private string[] name = new string[10];
            //定义索引器,必须以this关键字定义,设置name字段的索引值为0,address字段的索引值为1
            public string this[int index]
            {
                get
                {
                    return name[index];
                }
                set
                {
                    this.name[index] = value;
                }
            }
        }
    }
}

 

索引器允许类和结构的实例按照与数组相同的方式进行索引,索引器类似与属性,不同之处在于他们的访问器采用参数。被称为有参属性

多参数索引器案例(借鉴)

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

namespace 索引器2
{
    class Program
    {
        static void Main(string[] args)
        {
            ScoreIndex s = new ScoreIndex();
            s["张三", 1] = 90;
            s["张三", 2] = 100;
            s["张三", 3] = 80;
            s["李四", 1] = 60;
            s["李四", 2] = 70;
            s["李四", 3] = 50;
            Console.WriteLine("张三课程编号为1的成绩为:" + s["张三", 1]);
            Console.WriteLine("张三的所有成绩为:");
            ArrayList temp;
            temp = s["张三"];
            foreach (IndexClass b in temp)
            {
                Console.WriteLine("姓名:" + b.Name + "课程编号:" + b.CourseID + "分数:" + b.Score);
            }
            Console.ReadKey();
        }
        class IndexClass
        {
            private string _name;
            private int _courseid;
            private int _score;
            public IndexClass(string _name, int _courseid, int _score)
            {
                this._name = _name;
                this._courseid = _courseid;
                this._score = _score;
            }
            public string Name
            {
                get { return _name; }
                set { this._name = value; }
            }
            public int CourseID
            {
                get { return _courseid; }
                set { this._courseid = value; }
            }
            public int Score
            {
                get { return _score; }
                set { this._score = value; }
            }
        }

        class ScoreIndex
        {
            private ArrayList arr;
            public ScoreIndex()
            {
                arr = new ArrayList();
            }
            public int this[string _name, int _courseid]
            {
                get
                {
                    foreach (IndexClass a in arr)
                    {
                        if (a.Name == _name && a.CourseID == _courseid)
                        {
                            return a.Score;
                        }
                    }
                    return -1;
                }
                set
                {
                    arr.Add(new IndexClass(_name, _courseid, value)); //arr["张三",1]=90
                }
            }
            //重载索引器
            public ArrayList this[string _name]
            {
                get
                {
                    ArrayList temp = new ArrayList();
                    foreach (IndexClass b in arr)
                    {
                        if (b.Name == _name)
                        {
                            temp.Add(b);
                        }
                    }
                    return temp;
                }
            }
        }
    }
}

总结:

索引器:this 关键字用于定义索引器,Class或Struct只允许定义一个索引器

索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。 

get 访问器返回值。set 访问器分配值。 

value 关键字用于定义由 set 索引器分配的值。 

索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。 

索引器可被重载。 

索引器可以有多个形参,例如当访问二维数组时。 

索引器可以使用百数值下标,而数组只能使用整数下标:如下列定义一个String下标的索引器 
public int this [string name] {...} 

索引器与数组的比较:

索引器的索引值不受类型限制。用来访问数组的索引值一定是整数,而索引器可以是其他类型的索引值。

索引器允许重载,一个类可以有多个索引器。

索引器不是一个变量没有直接对应的数据存储地方。索引器有get和set访问器。

索引器与属性的比较

标识方式:属性以名称来标识,索引器以函数签名来标识。

索引器可以被重载。属性则不可以被重载。

属性可以为静态的,索引器属于实例成员,不能被声明为static

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值