c# 索引器(Dictionary赋值取值的方式)

1  什么是索引器?

  索引器提供了一种访问类或者结构的方法,即允许按照与数组相同的方式对类、结构或接口进行索引。

  例如:Dictionary(词典类)的赋值取值方式。

2 数字索引器

  2.1 定义一个索引器类

    public class University
    {
        private const int MAX = 10;
        private string[] names = new string[MAX];

        //索引器
        public string this[int index]
        {
            get
            {
                if(index >= 0 && index < MAX)
                {
                    return names[index];
                }
                return null;
            }
            set
            {
                if (index >= 0 && index < MAX)
                {
                    names[index] = value;
                }
            }
        }
    }

  2.2 使用这个索引器类

            University u = new University();
            u[0] = "测试大学0";
            u[1] = "测试大学1";
            u[2] = "测试大学2";
            System.Console.WriteLine(u[0]);
            System.Console.WriteLine(u[1]);
            System.Console.WriteLine(u[2]);

  2.3 话白

  是不是很像Dictionary(词典类)的赋值取值方式了?但这只能用数字来索引;

3 字符串索引器

  3.1 定义一个索引器类

    public class MySimpleDictionary
    {
        private List<KeyValueEntity> keyValueEntitys = new List<KeyValueEntity>();

        //索引器
        public object this[string key]
        {
            get
            {
                foreach(KeyValueEntity keyValueEntity in keyValueEntitys)
                {
                    if (keyValueEntity.key == key)
                    {
                        return keyValueEntity.value;
                    }
                }
                return null;
            }
            set
            {
                foreach (KeyValueEntity keyValueEntity in keyValueEntitys)
                {
                    if (keyValueEntity.key == key)
                    {
                        keyValueEntity.value = value;
                        //短路
                        return;
                    }
                }
                KeyValueEntity newKeyValueEntity = new KeyValueEntity();
                newKeyValueEntity.key = key;
                newKeyValueEntity.value = value;
                keyValueEntitys.Add(newKeyValueEntity);
            }
        }
    }

    public class KeyValueEntity
    {
        public string key { set; get; }
        public object value { set; get; }
    }

  3.2 使用这个索引器类

            MySimpleDictionary mydic = new MySimpleDictionary();
            mydic["testKey0"] = "testValue0";
            mydic["testKey1"] = "testValue1";
            mydic["testKey2"] = "testValue2";
            System.Console.WriteLine(mydic["testKey0"]);
            System.Console.WriteLine(mydic["testKey1"]);
            System.Console.WriteLine(mydic["testKey2"]);

  3.3 话白

    是不是更像Dictionary(词典类)的赋值取值方式了?但这只能用字符串来索引;

  接下来,我们在此基础上改造一个泛型的;

4 泛型索引器

  4.1 定义一个索引器类

    public class MySimpleDictionary<T, U>
    {
        private List<KeyValueEntity<T, U>> keyValueEntitys = new List<KeyValueEntity<T, U>>();

        //索引器
        public U this[T key]
        {
            get
            {
                foreach (KeyValueEntity<T, U> keyValueEntity in keyValueEntitys)
                {
                    if (keyValueEntity.key.Equals(key))
                    {
                        return keyValueEntity.value;
                    }
                }
                return default(U);
            }
            set
            {
                foreach (KeyValueEntity<T, U> keyValueEntity in keyValueEntitys)
                {
                    if (keyValueEntity.key.Equals(key))
                    {
                        keyValueEntity.value = value;
                        //短路
                        return;
                    }
                }
                KeyValueEntity<T, U> newKeyValueEntity = new KeyValueEntity<T, U>();
                newKeyValueEntity.key = key;
                newKeyValueEntity.value = value;
                keyValueEntitys.Add(newKeyValueEntity);
            }
        }
    }

    public class KeyValueEntity<T, U>
    {
        public T key { set; get; }
        public U value { set; get; }
    }

  4.2 使用这个索引器类

            MySimpleDictionary<string, string> dic = new MySimpleDictionary<string, string>();
            dic["testKey0"] = "testValue0";
            dic["testKey1"] = "testValue1";
            dic["testKey2"] = "testValue2";
            System.Console.WriteLine(dic["testKey0"]);
            System.Console.WriteLine(dic["testKey1"]);
            System.Console.WriteLine(dic["testKey2"]);

  4.3 白话

  到这里为止,我写的这个【简化版的Dictionary】跟【.net框架的Dictionary】相比还有一点较大的不同,如下:

    1)数据结构算法不同

    我写的词典类,是简单的键值对列表;

    框架的词典类,是动态扩展的数组+hash冲突解决算法,有兴趣的同学去看源码,毕竟我这篇博客主要讲的是索引器;

5 组合索引器

  5.1 定义一个索引器类

    public class MySimpleDictionary
    {
        private List<GroupKeyValueEntity> keyValueEntitys = new List<GroupKeyValueEntity>();

        //索引器
        public object this[string group, string key]
        {
            get
            {
                foreach (GroupKeyValueEntity keyValueEntity in keyValueEntitys)
                {
                    if (keyValueEntity.group == group && keyValueEntity.key == key)
                    {
                        return keyValueEntity.value;
                    }
                }
                return null;
            }
            set
            {
                foreach (GroupKeyValueEntity keyValueEntity in keyValueEntitys)
                {
                    if (keyValueEntity.group == group && keyValueEntity.key == key)
                    {
                        keyValueEntity.value = value;
                        //短路
                        return;
                    }
                }
                GroupKeyValueEntity newKeyValueEntity = new GroupKeyValueEntity();
                newKeyValueEntity.group = group;
                newKeyValueEntity.key = key;
                newKeyValueEntity.value = value;
                keyValueEntitys.Add(newKeyValueEntity);
            }
        }
    }

    public class GroupKeyValueEntity
    {
        public string group { set; get; }
        public string key { set; get; }
        public object value { set; get; }
    }

  5.2 使用这个索引器类

            MySimpleDictionary dic = new MySimpleDictionary();
            dic["group0", "key0"] = "value0";
            dic["group0", "key1"] = "value1";
            dic["group0", "key2"] = "value2";
            dic["group1", "key0"] = "value3";
            dic["group1", "key1"] = "value4";
            dic["group1", "key2"] = "value5";
            System.Console.WriteLine(dic["group0", "key0"]);
            System.Console.WriteLine(dic["group0", "key1"]);
            System.Console.WriteLine(dic["group0", "key2"]);
            System.Console.WriteLine(dic["group1", "key0"]);
            System.Console.WriteLine(dic["group1", "key1"]);
            System.Console.WriteLine(dic["group1", "key2"]);

  5.3 白话

  组合索引器不常用,对应的泛型就不在这里赘述了,有兴趣的同学不妨自己改造一下,毕竟泛型的用法还是很简单滴。。。

posted @ 2021-01-10 15:53  by-lhc  阅读(3299)  评论(0编辑  收藏  举报