您的位置:首页 > 编程语言 > C#

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

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

namespace ConsoleApplication1
{

    #region testClass
    class Person
    {
        private string name;

        public string Name
        {
            get { return name; }
        }
        private int age;

        public int Age
        {
            get { return age; }
        }
        private string title;

        public string Title
        {
            get { return title; }
        }
        public override string ToString()
        {

            if (title == null)
            {
                return "我叫"+name +" ,我今年 "+age +"岁了!";
            }
            else
            {
                return "我叫" + name + ", 我是" + title + " , " + age+"岁了";
            }
        }
        public Person(string name, int age)
        {
            this.age = age;
            this.name = name;
        }
        public Person(string name, int age, string title)
            : this(name, age)
        {
            this.title = title;
        }
    }

    #endregion

    #region IndexerClass

    class PersonCollection {
        //建立一个对象集合
        private System.Collections.ArrayList personArrayList = new System.Collections.ArrayList();

        public Person this[int index]
        {
            get
            {
                //索引不能超出上下界限
                if (!(index < 0 || index > personArrayList.Count - 1))
                {
                    return personArrayList[index] as Person;
                }
                else
                {
                    return null;
                }
              
            }
            set
            {
                personArrayList.Insert(index, value);
            }
        }
      //这里补充了一个根据人名字返回集合中第一个人的实例,如果名字不存在返回null
       public Person this[string name]{
            get{
                foreach(Person p in personArryList){
                       if(p.Name==name){
                               return p;
                       }
                       rerurn null;
                }
            }
       }
        //返回集合中的对象数量
        public int Count
        {
            get
            {
                return personArrayList.Count;
            }

        }
        public override string ToString()
        {
            System.Text.StringBuilder s = new System.Text.StringBuilder(string.Format("我们家{0}个人/n",this.Count));
            foreach (Person p in personArrayList)
            {
                s.Append(p.ToString()+"/n");
            }
            return s.ToString();
        }
 
    }
    #endregion

    #region Test
    class test
    {
        static void Main()
        {
            PersonCollection home = new PersonCollection();
            home[0] = new Person("张三", 34, "爸爸");
            home[1] = new Person("李四", 32, "妈妈");
            home[2] = new Person("张五", 12);
            Console.WriteLine(home[0].ToString());
            Console.WriteLine(home[1].ToString());
            Console.WriteLine(home[2].ToString());
                               Console.WriteLine(home["张三"].ToString());             Console.ReadKey();

            Console.WriteLine(home.ToString());
            Console.ReadKey();
        }
    }
    #endregion
}

下面是软件官方的例子的代码:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
   //将文件字节数组访问类
    class FileByteArray
    {
        //访问该文件的基础流
        System.IO.Stream stream;
        
        public FileByteArray(string fileName)
        {
            stream = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
        }
       //操作完关闭流
        public void CloseStream()
        {
            stream.Close();
            stream = null;
        }
        //对文件读写的索引器
        public byte this[long index]
        {
            get
            {
                byte[] buffer = new byte[1];
                stream.Seek(index, System.IO.SeekOrigin.Begin);
                stream.Read(buffer, 0, 1);
                return buffer[0];
            }
            set
            {
                byte[] buffer=new byte[1]{value};
                stream.Seek(index, System.IO.SeekOrigin.Begin);
                stream.Write(buffer, 0, 1);
            }
        }
        public long Length
        {
            get
            {
                return stream.Seek(0, System.IO.SeekOrigin.End);
            }
        }
    }
   //测试的作用就是吧传入的根目录下的文件1.txt中的字节进行反转
    public class test
    {
        static void Main(string[] args)
        {
            string fileName = "1.txt";
            if (args.Length != 1)
            {
                Console.WriteLine("参数太多");
                return;
            }
            if (!System.IO.File.Exists(fileName))
            {
                Console.WriteLine("文件不存在~");
                return;
            }
            FileByteArray file = new FileByteArray(fileName);
            long len = file.Length;
            for (long i = 0; i < len/2;++i )
            {
                byte t;
                t = file[i];
                file[i] = file[len - i - 1];
                file[i] = t;

            }
            file.CloseStream();
        }
    }
}

先就介绍这么多吧 更深的应用还在熟悉中~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: