索引器【C#】

索引:

索引,索的是实例化的编号,派生的子类,第   [ N ]   个儿子。

用数组的方式访问实例。 

返回的是实例的,一个属性值

声明:        this [ 索引 ]

    public string this[int index]
      {
         get
         {
           
         }
         set
         {
          
         }
      }


 public int this[string name]   //根据名字,确定实例编号
  {
         get
         {
            int index = 0;
            while(index < size)
            {
               if (namelist[index] == name)
               {
                return index;
               }
               index++;
            }
            return index;
         }

  }
using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string[] namelist = new string[size];
      static public int size = 10;
      public IndexedNames()
      {
         for (int i = 0; i < size; i++)
         namelist[i] = "N. A.";
      }
      public string this[int index]
      {
         get
         {
            string tmp;

            if( index >= 0 && index <= size-1 )
            {
               tmp = namelist[index];
            }
            else
            {
               tmp = "";
            }

            return ( tmp );
         }
         set
         {
            if( index >= 0 && index <= size-1 )
            {
               namelist[index] = value;
            }
         }
      }

      static void Main(string[] args)
      {
         IndexedNames names = new IndexedNames();
         names[0] = "Zara";
         names[1] = "Riz";
         names[2] = "Nuha";
         names[3] = "Asif";
         names[4] = "Davinder";
         names[5] = "Sunil";
         names[6] = "Rubic";
         for ( int i = 0; i < IndexedNames.size; i++ )
         {
            Console.WriteLine(names[i]);
         }
         Console.ReadKey();
      }
   }
}

2重载索引器(Indexer)

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string[] namelist = new string[size];
      static public int size = 10;
      public IndexedNames()
      {
         for (int i = 0; i < size; i++)
         {
          namelist[i] = "N. A.";
         }
      }
      public string this[int index]
      {
         get
         {
            string tmp;

            if( index >= 0 && index <= size-1 )
            {
               tmp = namelist[index];
            }
            else
            {
               tmp = "";
            }

            return ( tmp );
         }
         set
         {
            if( index >= 0 && index <= size-1 )
            {
               namelist[index] = value;
            }
         }
      }
      public int this[string name]
      {
         get
         {
            int index = 0;
            while(index < size)
            {
               if (namelist[index] == name)
               {
                return index;
               }
               index++;
            }
            return index;
         }

      }

      static void Main(string[] args)
      {
         IndexedNames names = new IndexedNames();
         names[0] = "Zara";
         names[1] = "Riz";
         names[2] = "Nuha";
         names[3] = "Asif";
         names[4] = "Davinder";
         names[5] = "Sunil";
         names[6] = "Rubic";
         // 使用带有 int 参数的第一个索引器
         for (int i = 0; i < IndexedNames.size; i++)
         {
            Console.WriteLine(names[i]);
         }
         // 使用带有 string 参数的第二个索引器
         Console.WriteLine(names["Nuha"]);
         Console.ReadKey();
      }
   }
}

3

4

5

相关推荐

  1. 索引C#】

    2023-12-06 17:30:03       37 阅读
  2. C# 接口_索引_命名空间

    2023-12-06 17:30:03       21 阅读
  3. C# 索引的范例和要点

    2023-12-06 17:30:03       20 阅读
  4. C#学习3--实验:索引和接口

    2023-12-06 17:30:03       15 阅读
  5. 2402C++,C++26包索引

    2023-12-06 17:30:03       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 17:30:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 17:30:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 17:30:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 17:30:03       20 阅读

热门阅读

  1. 特征与特征图的区别

    2023-12-06 17:30:03       32 阅读
  2. git 学习笔记

    2023-12-06 17:30:03       45 阅读
  3. 【开源视频联动物联网平台】Node-RED规则引擎

    2023-12-06 17:30:03       35 阅读
  4. 前端面试题:常见的响应状态码

    2023-12-06 17:30:03       37 阅读
  5. 【总结】ES 7.x 配置用户名和密码访问(亲测可用)

    2023-12-06 17:30:03       42 阅读
  6. C语言K&R圣经笔记 4.3 外部变量

    2023-12-06 17:30:03       24 阅读
  7. RabbitMQ常用命令(一)

    2023-12-06 17:30:03       28 阅读
  8. 大脑--学习方法

    2023-12-06 17:30:03       35 阅读
  9. Linux Camera Driver(3):DEBUG

    2023-12-06 17:30:03       35 阅读