zoukankan      html  css  js  c++  java
  • C#索引器封装

    1、学生数据类

      public class Student

        {

           public  Student()

            { }

           public  Student(string no,string name)

            {

                this.StudentNo = no;

                this.StudentName = name;

            }

            private string studentNo;

            private string studentName;

            /// <summary>

            /// 学号

            /// </summary>

            public string StudentNo

            {

                get

                {

                    return studentNo;

                }

                set

                {

                    studentNo = value;

                }

            }

            /// <summary>

            /// 姓名

            /// </summary>

            public string StudentName

            {

                get

                {

                    return studentName;

                }

                set

                {

                    studentName = value;

                }

            }

        }

    2、数组

        public class MyList

        {

            /// <summary>

            /// 数组元素实际个数

            /// </summary>

           private  int count;

           Student[] stus = new Student[4];

            #region 元素个数

            public int Count

            {

                get

                {

                    return count;

                }

                set

                {

                    count = value;

                }

            }

            #endregion

            #region 添加元素

            public void Add(Student item)

            {

                if (Count == stus.Length)

                {

                    Student[] newstus = new Student[stus.Length * 2];

                    stus.CopyTo(newstus, 0);

                    stus = newstus;

                }

                stus[count] = item;

                count++;

            }

            #endregion

            #region 获取元素

            public Student Get(int index)

            {

                return stus[index];

            }

            public Student Get(string name)

            {

                for (int i = 0; i < count; i++)

                {

                    if (stus[i].StudentName == name)

                    {

                        return stus[i];

                    }

                }

                return null;

            }

            #endregion

            #region 索引器

            public Student this[int index]

            {

                get

                {

                    return stus[index];

                }

                set { stus[index] = value; }

            }

            public Student this[string name]

            {

                get

                {

                    for (int i = 0; i < count; i++)

                    {

                        if (stus[i].StudentName == name)

                        {

                            return stus[i];

                        }

                    }

                    return null;

                }

                set

                {

                    for (int i = 0; i < count; i++)

                    {

                        if (stus[i].StudentName == name)

                        {

                            stus[i] = value;

                        }

                    }

                }

            }

            #endregion

        }

    3、调用

      static void Main(string[] args)

            {

                MyList ml = new c0index.MyList();

                ml.Add(new Student("001","aa"));

                ml.Add(new Student("002", "bb"));

                ml.Add(new Student("003", "cc"));

                ml.Add(new Student("004", "dd"));

                ml.Add(new Student("005", "ee"));

                for (int i = 0; i < ml.Count; i++)

                {

                    Console.WriteLine(ml[i].StudentName);

                }

            }

  • 相关阅读:
    《大数据——大价值、大机遇、大变革》试读
    div+csS中的一些技巧和浏览器兼容的办法
    js正则表达式
    布局 IE haslayout
    几个好的网站
    ie6 几个li上下排列会闪动的问题,嵌套div—外层div内层div都设置背景颜色,内层div背景色不显示的问题
    live()解决Jquery采用append添加的元素事件无效和获取不到添加元素的值
    SQL语句写返回一天内的纪录,得到一周内星期几的时间
    网易邮箱添加附件功能原理浅析转
    实体类序列化
  • 原文地址:https://www.cnblogs.com/pengJk/p/6346718.html
Copyright © 2011-2022 走看看