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);

                }

            }

  • 相关阅读:
    python 替换图片中的像素
    python paddleocr 增加识别速度的方法
    mysql 子查询少用
    mysql 临时表代替in的方法
    mysql创建临时表不用创建临时表结构的方法
    Nginx 报错 connect() failed (110: Connection timed out) while connecting to upstream
    用mkcert生成SSL,内网完美起跑https
    用于制作任何echarts地图(省、市、区、城镇街道)
    javascript执行机制
    ES6便捷语法
  • 原文地址:https://www.cnblogs.com/pengJk/p/6346718.html
Copyright © 2011-2022 走看看