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 白話

  組合索引器不常用,對應的泛型就不在這里贅述了,有興趣的同學不妨自己改造一下,畢竟泛型的用法還是很簡單滴。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM