• 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;
                }
            }
        }
    }

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

  • 相关阅读:
    [转] 微服务架构
    vue拦截器实现统一token,并兼容IE9验证
    一句话 道出设计模式
    老话闲说,关于身份证号码的验证
    值得珍藏的HTTP协议详解
    微信内嵌H5网页 解决js倒计时失效
    使用JavaScript重定向URL参数
    Nodejs --我自己的学习笔记
    ONLY三行脚本 SQL数据恢复到指定时间点
    [转] 数据库加锁 sql加锁的
  • 原文地址:https://www.cnblogs.com/Tanqurey/p/12259551.html
Copyright © 2020-2023  润新知