c#语言基础(4)----索引器

索引器也叫称为:参数化成员属性,就像成员属性一样,它在类中申明,个体中可以和属性一样获取和设置,  但和属性不同的地方:可以接受一个或者多个参数,而且使用this作为索引器名。注意:索引器不能使用static修饰,因为索引器只适用于实例。
作用:提供了一种访问途径,访问一个存在实例中的集合的途径
一个类可以有多个索引器,但是他的签名必须不同(这里说的是索引的参数类型应该不同)

下面是一个索引的简单例子:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApplication1
  5. {
  6.     #region testClass
  7.     class Person
  8.     {
  9.         private string name;
  10.         public string Name
  11.         {
  12.             get { return name; }
  13.         }
  14.         private int age;
  15.         public int Age
  16.         {
  17.             get { return age; }
  18.         }
  19.         private string title;
  20.         public string Title
  21.         {
  22.             get { return title; }
  23.         }
  24.         public override string ToString()
  25.         {
  26.             if (title == null)
  27.             {
  28.                 return "我叫"+name +" ,我今年 "+age +"岁了!";
  29.             }
  30.             else
  31.             {
  32.                 return "我叫" + name + ", 我是" + title + " , " + age+"岁了";
  33.             }
  34.         }
  35.         public Person(string name, int age)
  36.         {
  37.             this.age = age;
  38.             this.name = name;
  39.         }
  40.         public Person(string name, int age, string title)
  41.             : this(name, age)
  42.         {
  43.             this.title = title;
  44.         }
  45.     }
  46.     #endregion
  47.     #region IndexerClass
  48.     class PersonCollection {
  49.         //建立一个对象集合
  50.         private System.Collections.ArrayList personArrayList = new System.Collections.ArrayList();
  51.         public Person this[int index]
  52.         {
  53.             get
  54.             {
  55.                 //索引不能超出上下界限
  56.                 if (!(index < 0 || index > personArrayList.Count - 1))
  57.                 {
  58.                     return personArrayList[index] as Person;
  59.                 }
  60.                 else
  61.                 {
  62.                     return null;
  63.                 }
  64.               
  65.             }
  66.             set
  67.             {
  68.                 personArrayList.Insert(index, value);
  69.             }
  70.         }
  71.       //这里补充了一个根据人名字返回集合中第一个人的实例,如果名字不存在返回null
  72.        public Person this[string name]{
  73.             get{
  74.                 foreach(Person p in personArryList){
  75.                        if(p.Name==name){
  76.                                return p;
  77.                        }
  78.                        rerurn null;
  79.                 }
  80.             }
  81.        }
  82.         //返回集合中的对象数量
  83.         public int Count
  84.         {
  85.             get
  86.             {
  87.                 return personArrayList.Count;
  88.             }
  89.         }
  90.         public override string ToString()
  91.         {
  92.             System.Text.StringBuilder s = new System.Text.StringBuilder(string.Format("我们家{0}个人/n",this.Count));
  93.             foreach (Person p in personArrayList)
  94.             {
  95.                 s.Append(p.ToString()+"/n");
  96.             }
  97.             return s.ToString();
  98.         }
  99.  
  100.     }
  101.     #endregion
  102.     #region Test
  103.     class test
  104.     {
  105.         static void Main()
  106.         {
  107.             PersonCollection home = new PersonCollection();
  108.             home[0] = new Person("张三", 34, "爸爸");
  109.             home[1] = new Person("李四", 32, "妈妈");
  110.             home[2] = new Person("张五", 12);
  111.             Console.WriteLine(home[0].ToString());
  112.             Console.WriteLine(home[1].ToString());
  113.             Console.WriteLine(home[2].ToString());
                               Console.WriteLine(home["张三"].ToString()); 
  1.             Console.ReadKey();
  2.             Console.WriteLine(home.ToString());
  3.             Console.ReadKey();
  4.         }
  5.     }
  6.     #endregion
  7. }
下面是软件官方的例子的代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApplication1
  5. {
  6.    //将文件字节数组访问类
  7.     class FileByteArray
  8.     {
  9.         //访问该文件的基础流
  10.         System.IO.Stream stream;
  11.         
  12.         public FileByteArray(string fileName)
  13.         {
  14.             stream = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
  15.         }
  16.        //操作完关闭流
  17.         public void CloseStream()
  18.         {
  19.             stream.Close();
  20.             stream = null;
  21.         }
  22.         //对文件读写的索引器
  23.         public byte this[long index]
  24.         {
  25.             get
  26.             {
  27.                 byte[] buffer = new byte[1];
  28.                 stream.Seek(index, System.IO.SeekOrigin.Begin);
  29.                 stream.Read(buffer, 0, 1);
  30.                 return buffer[0];
  31.             }
  32.             set
  33.             {
  34.                 byte[] buffer=new byte[1]{value};
  35.                 stream.Seek(index, System.IO.SeekOrigin.Begin);
  36.                 stream.Write(buffer, 0, 1);
  37.             }
  38.         }
  39.         public long Length
  40.         {
  41.             get
  42.             {
  43.                 return stream.Seek(0, System.IO.SeekOrigin.End);
  44.             }
  45.         }
  46.     }
  47.    //测试的作用就是吧传入的根目录下的文件1.txt中的字节进行反转
  48.     public class test
  49.     {
  50.         static void Main(string[] args)
  51.         {
  52.             string fileName = "1.txt";
  53.             if (args.Length != 1)
  54.             {
  55.                 Console.WriteLine("参数太多");
  56.                 return;
  57.             }
  58.             if (!System.IO.File.Exists(fileName))
  59.             {
  60.                 Console.WriteLine("文件不存在~");
  61.                 return;
  62.             }
  63.             FileByteArray file = new FileByteArray(fileName);
  64.             long len = file.Length;
  65.             for (long i = 0; i < len/2;++i )
  66.             {
  67.                 byte t;
  68.                 t = file[i];
  69.                 file[i] = file[len - i - 1];
  70.                 file[i] = t;
  71.             }
  72.             file.CloseStream();
  73.         }
  74.     }
  75. }


先就介绍这么多吧 更深的应用还在熟悉中~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值