温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

c#索引器

发布时间:2020-07-06 15:15:14 来源:网络 阅读:369 作者:Liu_guozhu 栏目:开发技术

C#索引器


一:什么是C#的索引器:
     是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写。


二:C#"索引器”典型结构

c#索引器


为了更好的说明 C#“索引器”的功能,参考以下代码:


    class Demo1
    {
        //容纳0个整数的数组
        private int[] _Array = new int[10];

        /// <summary>
        /// 定义索引器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int this[int index]
        {
            set
            {
                if (index >= 0 && index <= 10)
                {
                    _Array[index] = value;
                }
            }

            get
            {
                if (index < 0 || index >= 100)
                {
                    return 0;
                }
                else
                {
                    return _Array[index];
                }
            }
        }

        static void Main1(string[] args)
        {
            Demo1 obj = new Demo1();
            obj[0] = 1000;
            obj[1] = 2000;

            //输出
            Console.WriteLine(obj[0]);
            Console.WriteLine(obj[1]);
        }
    }


三:C#"索引器”与数组的区别
    1:  索引器的索引值类型不限定为整数
    2:索引器允许重载
    3:索引器具有Get 与 Set 访问器。
 
四: C#"索引器”与属性的区别
    1:索引器以函数签名方式this 来标识,而属性采用名称(名称可以任意)来标识。
    2:索引器可以重载,属性不能。
    3:索引器不能用Static ,而属性可以。


索引器允许重载,给了更清晰的表述,请看如下示例:


    class Demo2
    {
        //容纳0个整数的数组
        private Hashtable _HT = new Hashtable();
        private int[] _Array = new int[10];

        /// <summary>
        /// 定义索引器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public string this[string index]
        {
            set
            {
                _HT.Add(index,value);
            }

            get
            {
                return _HT[index].ToString();
            }
        }

        /// <summary>
        /// 索引器重载
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public int this[int index]
        {
            set
            {
                if (index >= 0 && index <= 10)
                {
                    _Array[index] = value;
                }
            }

            get
            {
                if (index < 0 || index >= 100)
                {
                    return 0;
                }
                else
                {
                    return _Array[index];
                }
            }
        }

        static void Main(string[] args)
        {
            Demo2 obj = new Demo2();
            obj["zhangSan"] = "张三";
            obj["lisi"] = "李四";
            obj[1] = 3000;
            obj[2] = 4000;

            //输出
            Console.WriteLine(obj["zhangSan"]);
            Console.WriteLine(obj["lisi"]);
            Console.WriteLine(obj[1]);
            Console.WriteLine(obj[2]);
        }
    }


总结:

    C#的索引器的使用概率不是很高,一般都集中在游戏开发人员(程序员)需要开发自己的“自定义集合”类型的时候使用,笔者在下一章节来给大家介绍如何开发自己的“自定义集合”。请大家围观。


  2016年9月5日


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI