C#数组GetSet索引器分析

来源:互联网 发布:硬盘文件恢复软件 编辑:程序博客网 时间:2024/06/14 06:00

C#数组GetSet索引器分析

源码均在文末地址给出。


有时候需要对数组的数据进行封装控制,该怎么处理数组数据呢?设置数组的GetSet方法还是弄一个索引器来进行控制。接下来进行分别测试。


属性

C#中的属性,是对类内部的字段进行进一步的封装控制,安全性考虑。通过属性设置可以控制字段的读写控制,以及内部的进一步特殊处理。(如非法字符处理 越界处理等)
属性的定义:
访问修饰符 返回类型 属性名

get{语句集合}
set{语句集合}

普通字段的属性

如学生类中姓名字段

  public class Student  {      private string name;      /// <summary>      /// 定义学生的姓名属性      /// </summary>      public string Name      {          get { return name; }          set { name = value; }      }   }    class Program    {        static void Main(string[] args)        {           Student student = new Student();           student.Name = "Jeff Wong";           //获取student.Name将执行属性的Get方法           //"Jeff Wong"执行属性的Set方法  从右到左进行           Console.WriteLine(student.Name);           Console.Read();        }    }

数组类型的属性(与集合类型属性相似)

    class Test    {        private int[] ids=new int[5];        //属性封装的是整个数据对象 不是具体的单独类型        public int[] Ids        {            get            {                return ids;            }            //set            //{            //    ids = value;            //}        }    }    class Program    {        static void Main(string[] args)        {            Test t = new Test();            int[] arr = new int[5] { 1, 2, 3, 4, 5 };            //t.Ids = arr;//会调用属性的Set方法            t.Ids[1] = 1;//正常访问数组  调用属性的Get方法  返回对象给t.Ids               //跟下标无关 下标赋值是数组自己的功能            for (int i = 0; i < t.Ids.Length; i++)            {                Console.WriteLine(t.Ids[i]);            }            Console.Read();        }    }

索引器

索引器用于以更便捷的方式访问对象中包含的成员数组或集合。所谓索引器就是一类特殊的属性,通过它们你就可以像引用数组一样引用自己的类。
索引器结构
访问修饰符 返回类型 this[参数类型 参数…]

get{语句集合}
set{语句集合}

索引器使得对象可按照与数组相似的方法进行索引。
this 关键字用于定义索引器。
get 访问器返回值。set 访问器分配值。
value 关键字用于定义由 set 索引器分配的值。
索引器不必根据整数值进行索引,由你决定如何定义特定的查找机制。

数组类型的索引器(举例使用 一般会使用集合)

 class Test {     private int[] ids=new int[5];     public int this[int nindex]     {         get { return ids[nindex]; }         set { ids[nindex] = value; }     } } static void Main(string[] args)  {      Test t = new Test();      int[] arr = new int[5] { 1, 2, 3, 4, 5 };      t[2]=6;//索引器访问数组      for (int i = 0; i < t.Ids.Length; i++)      {          Console.WriteLine(t.Ids[i]);      }      Console.Read();  }

集合类型的索引器(通常是包含自身的类型)

索引器还支持进行多个参数,可以进行不同的重载 在此不做分析了

  public class Student    {        private string name;        /// <summary>        /// 定义学生的姓名属性        /// </summary>        public string Name        {            get { return name; }            set { name = value; }        }        public Student(string str)        {            this.Name = str;        }        public Student()        {            this.listStudents.Add(new Student("jeff wong"));            this.listStudents.Add(new Student("jeffery zhao"));            this.listStudents.Add(new Student("terry lee"));            this.listStudents.Add(new Student("dudu"));        }        private List<Student> listStudents = new List<Student>();        public List<Student> ListStudents        {            get            {                return listStudents;            }            set            {                listStudents = value;            }        }        /// <summary>        /// 构建索引器        /// </summary>        /// <param name="i"></param>        /// <returns></returns>        public Student this[int i]        {            get { return listStudents[i]; }            set { listStudents[i] = value; }        }    }    class Program    {        static void Main(string[] args)        {            Student student = new Student();            int num = student.ListStudents.Count;            Console.WriteLine("All the students:\nLeft-Attributes Right-Indexer\n");            for (int i = 0; i < num; i++)            {                Console.Write(student.ListStudents[i].Name); //通过属性,取所有学生名                Console.Write("-----------");                 Console.WriteLine(student[i].Name); //通过索引器,取所有学生名            }        }    }

实例源代码
GitHub地址
百度云地址:链接: https://pan.baidu.com/s/1kUQCVTt 密码: 2hnd

原创粉丝点击