zoukankan      html  css  js  c++  java
  • 索引器和类数组以及属性的区别

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using System.Windows.Forms;

    namespace @string
    {
        class Program
        {
            static void Main(string[] args)
            {
                change[] arri=new change[10];
                arri[0]=new change(1);
                arri[1]=new change(2);
                arri[2]=new change(3);
                arri[3]=new change(4);
                arri[4]=new change(5);
                Console.WriteLine(arri[0].Id);
                Console.WriteLine(arri[1].Id);
                Console.WriteLine(arri[2].Id);
                Console.WriteLine(arri[3].Id);
                Console.WriteLine(arri[4].Id);
                index indexertest = new index();
                indexertest[0] = 6;
                indexertest[1] = 7;
                indexertest[2] = 8;
                indexertest["3"] = 9;
                indexertest["4"] = 10;
                Console.WriteLine(indexertest[0]);
                Console.WriteLine( indexertest[1]);
                Console.WriteLine( indexertest[2]);
                Console.WriteLine(indexertest[3]);
                Console.WriteLine(indexertest[4]);
            }
        }
        class change//没有用indexer
        {
           
            private readonly int _id;

            public change(int i)
            {
                this._id=i;
            }
            public int Id
            {
                get { return _id; }
            }
        }
        class index//用indexer
        {
            int[] arri2 = new int[10];
            public int this[int index]
            {
        get { return arri2[index];}
        set { arri2[index] = value;}
              
            }
            public int this[string index1]//indexer重载
            {
                get { return arri2[int.Parse(index1)]; }
                set { arri2[int.Parse(index1)] = value; }
            }
        }
    }
         索引器的结构和属性相当的类似,但是它可以添加参数,而属性不可以。索引器有返回值类型而属性没有。

    另一方面索引器的功能右相当于类数组,但是索引器优于数值的是它的下标可以不为整型,而数组必须为整形。

    还有就是索引器可以被重载。

  • 相关阅读:
    让自己的网站或博客被百度收录的小技巧
    Linux cp一个文件夹时提示cp: omitting directory `test/'
    svn checkout 提示“由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。”解决方法
    Linux iptables开放特定端口
    CentOS7使用iptables防火墙开放端口
    Linux平台的SVN服务器的配置及搭建
    官网svn提交到代码库,但是不能同步到web目录
    【LINUX】SVN 代码提交之后。同步到web目录下
    React Native For Android 架构初探
    腾讯QQ会员技术团队:以手机QQ会员H5加速为例,为你揭开sonic技术内幕
  • 原文地址:https://www.cnblogs.com/jessie/p/1440925.html
Copyright © 2011-2022 走看看