索引器提供了一种可以让类被当作数组进行访问的方式。在C#中,类索引器是通过this的属性实现的。
index.ToString("D2")将index转换成一个具有两个字符宽度的字符串

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassIndexer
{
    /// <summary>
    /// define a class called AClass,which has only one private variable
    /// </summary>
   class AClass {
       private string _name;
       public string Name {
           get { return this._name; }
           set { this._name = value; }
       }
       public AClass(string name) {
           this._name = name;
       }
    }
    /// <summary>
    /// AClassContainer class
    /// </summary>
   class AClassContainer {
       private AClass[] _AClassList;
       public AClassContainer() {
           this._AClassList = new AClass[10];
       }
       public AClassContainer(int cap) {
           this._AClassList = new AClass[cap];
       }
       /// <summary>
       /// class indexer by using int 
       /// </summary>
       /// <param name="index"></param>
       /// <returns></returns>
       public AClass this[int index] {
           get {
               if ((index < 0) || (index > this._AClassList.Length)) return null;
               else return this._AClassList[index];
           }
           set {
               if ((index < 0) || (index > this._AClassList.Length)) return;
               else this._AClassList[index] = value;
           }
       }
       /// <summary>
       /// class indexer by using string
       /// </summary>
       /// <param name="name"></param>
       /// <returns></returns>
       public AClass this[string name] {
           get {
               foreach (AClass cls in this._AClassList) {
                   if (cls.Name == name) return cls;
               }
               return null;
           }
       }
   }
    class Program
    {
        static void Main(string[] args)
        {
            int count = 10;
            AClassContainer classContainer = new AClassContainer(count);
            for (int index = 0; index < count; index++) {
                string name = index.ToString("D2");
                classContainer[index] = new AClass(name);
            }
            for (int index = 0; index < count; index++)
            {
                AClass cls = classContainer[index];
                System.Console.WriteLine("classContainer[{0}].Name=\"{1}\"",index,cls.Name);
            }
            System.Console.WriteLine();
            for (int index = count - 1; index >= 0; index--) {
                string name = index.ToString("D2");
                AClass cls = classContainer[name];
                System.Console.WriteLine("classContainer[{0}].Name=\"{1}\"",name,cls.Name);
            }
            System.Console.ReadLine();
        }
    }
}

结果:
classContainer[0].Name="00"
classContainer[1].Name="01"
classContainer[2].Name="02"
classContainer[3].Name="03"
classContainer[4].Name="04"
classContainer[5].Name="05"
classContainer[6].Name="06"
classContainer[7].Name="07"
classContainer[8].Name="08"
classContainer[9].Name="09"

classContainer[09].Name="09"
classContainer[08].Name="08"
classContainer[07].Name="07"
classContainer[06].Name="06"
classContainer[05].Name="05"
classContainer[04].Name="04"
classContainer[03].Name="03"
classContainer[02].Name="02"
classContainer[01].Name="01"
classContainer[00].Name="00"

C#类索引器的使用的更多相关文章

  1. c#编程指南(六) 类索引器(Class Indexer)

    类索引器,可以使得你使用数组一样的方式来访问类的数据. 这种访问多见于数组,列表,词典,哈希表的快捷访问. 实际上写法很简单,写成:public T1 this[T2 i] 代码如下: using S ...

  2. 描述一下C#中索引器的实现过程,是否只能根据数字进行索引?

    不是.可以用任意类型. 索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了 ...

  3. C# 索引器简介

    索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,是程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了数组和集合成员时,索引器 ...

  4. 索引器 C#

    概述 索引器允许类或结构的实例就像数组一样进行索引. 索引器类似于属性,不同之处在于它们的访问器采用参数. 在下面的示例中,定义了一个泛型类,并为其提供了简单的 get 和 set 访问器方法(作为分 ...

  5. C#中的索引器的简单理解和用法

    索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了数组和集合成员时,索引器 ...

  6. C#入门--索引器

    C#入门--索引器 索引器允许类或结构的实例按照与数组相同的方式进行索引.索引器类似于属性,不同之处在于它们的访问器采用参数. 索引器概述 索引器使得对象可按照与数组相似的方法进行索引. get 访问 ...

  7. C# 接口中的索引器

    索引器可在 接口(C# 参考) 上声明.接口索引器的访问器与类索引器的访问器具有以下方面的不同: 接口访问器不使用修饰符. 接口访问器没有体. 因此,访问器的用途是指示索引器是读写.只读还是只写.以下 ...

  8. C# 类中索引器的使用二

    索引器(Indexer)是C#引入的一个新型的类成员,它使得类中的对象可以像数组那样方便.直观的被引用.索引器非常类似于属性,但索引器可以有参数列表,且只能作用在实例对象上,而不能在类上直接作用.定义 ...

  9. C# 类如何声明索引器以提供对类的类似数组的访问的代码

    研发期间,将内容过程中比较常用的内容段做个收藏,如下内容内容是关于 C# 类如何声明索引器以提供对类的类似数组的访问.的内容,希望能对各位有用处. using System;using System. ...

随机推荐

  1. VS2013密匙

    在网上找到的,亲测有用: BWG7X-J98B3-W34RT-33B3R-JVYW9

  2. ecshop 二次开发及模板标签

    ecs_account_log // 用户账目日志表   ecs_activity // 活动表(代码,名称,开始,结束,描述)   ecs_ad // 广告表(位置,类型,名称,链接,图片,开始,结 ...

  3. Iwpriv工作流程及常用命令使用之二

    iwpriv工具通过ioctl动态获取相应无线网卡驱动的private_args所有扩展参数 iwpriv是处理下面的wlan_private_args的所有扩展命令,iwpriv的实现上,是这样的, ...

  4. UI篇--Android中3种方法实现back键动作

    方法一:重写onBackPressed方法 @Override public void onBackPressed() { // do something what you want super.on ...

  5. IOS NSNotificationCenter 通知的使用

    1.注册通知 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notify) name:@" ...

  6. jquery的each()函数用法

    each()方法能使DOM循环结构简洁,不容易出错.each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组.多维数组.DOM, JSON 等等 在javaScript开发过程中使用 ...

  7. 使用std::function 把类成员函数指针转换为普通函数指针

    前言 这是改造前一篇 设计模式 的基础,使通知者不必知道观察者的类名和函数名,只需要知道更新函数的原型即可. 开发环境:WIN7 32位 + VS2010 发现在VS2005中使用std::funti ...

  8. bzoj 3091 城市旅行(LCT+数学分析)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3091 [思路] 膜Popoqqq大爷的题解 click here [代码]是坑... ...

  9. [Hive - LanguageManual] Create/Drop/Alter Database Create/Drop/Truncate Table

    Hive Data Definition Language Hive Data Definition Language Overview Create/Drop/Alter Database Crea ...

  10. 怎么利用SQL语句查询数据库中具体某个字段的重复行

    select * from [tablename] group by SeriNohaving count(SeriNo)<>1