zoukankan      html  css  js  c++  java
  • C#的索引器

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    
    namespace codeTest
    {
    
        class Program
        {
    
            static void Main(string[] args)
            {
                IndexTest indexname = new IndexTest();
                indexname[0] = "0";
                indexname[1] = "1";
                indexname[2] = "2";
                indexname[3] = "3";
                indexname[4] = "4";
                indexname[5] = "5";
                indexname[6] = "6";
                indexname[7] = "7";
                indexname[8] = "8";
                indexname[9] = "9";
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(indexname[i]); 
                }
    
                Console.WriteLine(indexname["1"]);
                Console.WriteLine(indexname["2"]);
                Console.WriteLine(indexname["6"]);
                IndexTest1 indextest1 = new IndexTest1();
                Console.WriteLine(indextest1[1]);
                Console.WriteLine(indextest1[2]);
                Console.WriteLine(indextest1[3]);
                Console.ReadLine();
            }
    
            public class IndexTest
            {
                string[] indexName;
                public IndexTest()
                {
                    indexName = new string[10] { "", "", "", "", "", "", "", "", "", "" };
                }
    
                public string this[int index]
                {
                    get
                    {
                        string tmp;
                        if (index >= 0 && index <= 10)
                        {
                            tmp = indexName[index];
                        }
                        else
                        {
                            tmp = "";
                        }
                        return tmp;
                    }
                    set
                    {
                        if (index >= 0 && index <= 10)
                        {
                            indexName[index] = value;
                        }
                    }
                }
    
                public int this[string name]
                {
                    get
                    {
                        int index = 0;
                        while (index < indexName.Length)
                        {
                            if (indexName[index] == name)
                            {
                                return index;
                            }
                            index++;
                        }
    
                        return -1;
                    }
                }
    
            }
    
            public interface IIndexTest
            {
                int this[int index]
                {
                    get;
                    set;
                }
            }
    
            public class IndexTest1 : IIndexTest
            {
                int[] array = new int[5] { 1, 2, 3, 4, 5 };
    
                public int this[int index]
                {
                    get
                    {
                        return array[index];
                    }
                    set
                    {
                        array[index] = value;
                    }
                }
            }
    
        }
    
    
    }
  • 相关阅读:
    方法的封装与调用(十)
    适配器设计模式及GenericServlet(九)
    错误页设置,设置HTTP状态码404,500(八)
    设置默认首页(七)
    ServletContext接口(六)
    javax.servlet.ServletConfig接口(五)
    C语言第2天基本运算
    再议extern和include的作用
    C语言中的++和--
    C语言培训第一天
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4765229.html
Copyright © 2011-2022 走看看