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

    实际中不使用这个东西,只做了解

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 索引器 {
        class Program {
            static void Main(string[] args) {
                person p = new person();
                p[0] = 1;
                p[1] = 2;
                p[2] = 3;
                p[3] = 4;
                p[4] = 5;
                foreach(var item in p.Numbers) {
                    Console.WriteLine(item);
                }
                p["0"] = "1";
                p["1"] = "2";
                p["2"] = "3";
                p["3"] = "4";
                p["4"] = "5";
                Console.WriteLine(p["0"]);
                Console.ReadLine();
            }
        }
        class person {
           
            private int[] _numbers = new int[5];
            public int[] Numbers {
                get { return _numbers; }
                set { _numbers = value; }
            }
            //索引器,让对象以索引的方式操作数组
            public int this[int index] {
                get { return _numbers[index]; }
                set { _numbers[index] = value; }
            }
    
            Dictionary<string, string> dic = new Dictionary<string, string>();
            public string this[string index] {
                set { dic[index] = value; }
                get { return dic[index]; }
            }
        }
    }
  • 相关阅读:
    前端之JavaScript
    前端之CSS
    前端之HTML
    编程总结
    线程
    锁机制,信号机制,事件机制
    并发编程
    struct
    linux查看端口
    vue页面跳转传参
  • 原文地址:https://www.cnblogs.com/liuguan/p/6246743.html
Copyright © 2011-2022 走看看