zoukankan      html  css  js  c++  java
  • MVC08

    1. c# 索引器(indexer)

    using System;
    using System.IO;
    
    namespace IO
    {
        class Program
        {
            private string[] nameList = new string[10];
            static void Main(string[] args)
            {
    
                var names = new IndexedNames();
                names[0] = "hi";
                Console.WriteLine(names[0]);
                Console.WriteLine(names[1]);
                Console.WriteLine(names[11]);
            }
    
            class IndexedNames
            {
                private string[] nameList = new string[10];
                public IndexedNames()
                {
                    for (int i = 0; i < nameList.Length; i++)
                    {
                        nameList[i] = "N/A";
                    }
                }
    
                public string this[int index]
                {
                    get
                    {
                        string tmp;
                        if (index >= 0 && index < nameList.Length)
                        {
                            tmp = nameList[index];
                        }
                        else
                        {
                            tmp = "index is empty";
                        }
                        return tmp;
                    }
    
                    set
                    {
                        if(index >= 0 && index < nameList.Length)
                        {
                            nameList[index] = value;
                        }
                    }
    
                }
            }
    
        }
    }
    using System;
    using System.IO;
    
    namespace IO
    {
        class Program
        {
            private string[] nameList = new string[10];
            static void Main(string[] args)
            {
    
                var names = new IndexedNames();
                names[0] = "hi";
                Console.WriteLine(names[0]);
                Console.WriteLine(names[1]);
                Console.WriteLine(names[11]);
    
                var testStr = "hi";
                Console.WriteLine(names[testStr]);
    
            }
    
            class IndexedNames
            {
                private string[] nameList = new string[10];
                public IndexedNames()
                {
                    for (int i = 0; i < nameList.Length; i++)
                    {
                        nameList[i] = "N/A";
                    }
                }
    
                public string this[int index]
                {
                    get
                    {
                        string tmp;
                        if (index >= 0 && index < nameList.Length)
                        {
                            tmp = nameList[index];
                        }
                        else
                        {
                            tmp = "index is empty";
                        }
                        return tmp;
                    }
    
                    set
                    {
                        if(index >= 0 && index < nameList.Length)
                        {
                            nameList[index] = value;
                        }
                    }
    
                }
    
                // 以字符串为索引,返回该字符串在数组中的整型索引,重载了索引器
                public int this[string name]
                {
                    get
                    {
                        int i = 0;
                        while(i < nameList.Length)
                        {
                            if(nameList[i] == name)
                            {
                                return i;
                            }
                        }
                        return -1;
                    }
                }
            }
    
        }
    }

     可为get、set 设置修饰符,一般set为private,get为public

    3. 基于接口的索引器与代码强壮性

    在接口内也可以新建索引器。

    下方

    using System;
    using System.IO;
    
    namespace IO
    {
        class Program
        {
            private string[] nameList = new string[10];
            static void Main(string[] args)
            {
    
                var names = new IndexedNames();
                names[0] = "hi";
                Console.WriteLine(names[0]);
                Console.WriteLine(names[1]);
                Console.WriteLine(names[11]);
    
                var testStr = "hi";
                Console.WriteLine(names[testStr]);
    
            }
        }
        class IndexedNames
        {
            private string[] nameList = new string[10];
            public IndexedNames()
            {
                for (int i = 0; i < nameList.Length; i++)
                {
                    nameList[i] = "N/A";
                }
            }
    
            public string this[int index]
            {
                get
                {
                    string tmp;
                    if (index >= 0 && index < nameList.Length)
                    {
                        tmp = nameList[index];
                    }
                    else
                    {
                        tmp = "index is empty";
                    }
                    return tmp;
                }
    
                set
                {
                    if (index >= 0 && index < nameList.Length)
                    {
                        nameList[index] = value;
                    }
                }
    
            }
    
            // 以字符串为索引,返回该字符串在数组中的整型索引
            public int this[string name]
            {
                get
                {
                    int i = 0;
                    while (i < nameList.Length)
                    {
                        if (nameList[i] == name)
                        {
                            return i;
                        }
                    }
                    return -1;
                }
            }
        }
        public interface ISomeInterface
        {
            int this[int index]
            {
                get;
                set;
            }
        }
        class IndexerClass : ISomeInterface
        {
            private int[] arr = new int[100];
            public int this[int index]
            {
                get
                {
                    return arr[index];
                }
                set
                {
                    arr[index] = value;
                }
            }
        }
    }

    ------------恢复内容结束------------

  • 相关阅读:
    React在componentDidMount里面发送请求
    React 术语词汇表
    React里受控与非受控组件
    React和Vue等框架什么时候操作DOM
    【LeetCode】79. Word Search
    【LeetCode】91. Decode Ways
    【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
    【LeetCode】1. Two Sum
    【LeetCode】141. Linked List Cycle (2 solutions)
    【LeetCode】120. Triangle (3 solutions)
  • 原文地址:https://www.cnblogs.com/Tanqurey/p/12259551.html
Copyright © 2011-2022 走看看