当前位置: 移动技术网 > IT编程>开发语言>.net > C#索引器与数组的区别

C#索引器与数组的区别

2019年09月18日  | 移动技术网IT编程  | 我要评论

好课件,对视吉他谱,区域情交友网

1.索引器的索引值类型不限定为整数

2.索引器允许重载

3.索引器不是一个变量

4.索引器以函数签名方式this标识,而属性采用名称来标识,名称可以任意

5.索引器不能使用static来进行声明,属性可以。索引器永远属于实例成员,因此不能声明为static。

using system;
using system.collections.generic;

namespace 编码练习
{
    public class student {
        public string  name { get;set;}
        public int courseid { get; set; }
        public int score { get; set; }

    }
    public class findscore
    {
        private list<student> student { get; set; }
        public findscore()
        {
            student = new list<student>();
        }
        public int this[string name,int courseid] {
            get {
                var s = student.find(x=>x.name== name&&x.courseid==courseid);
                if (s!=null)
                {
                    return s.score;
                }
                return -1;
            }
            set {
                student.add(new student() {name=name,courseid=courseid,score=value });
            }
        }
        //搜索
        public list<student> this[string name]
        {
            get
            {
                list<student> liststudents = student.findall(x => x.name == name);
                return liststudents;
            }
        }

        public static void main() {
            findscore fstudent = new findscore();
            fstudent["zhangsan", 1] = 98;
            fstudent["zhangsan", 2] = 100;
            fstudent["lisi", 1] = 80;
            fstudent["zhangsan", 3] = 90;
            //查询李四的成绩
            console.writeline("李四 课程编号2 成绩为:" + fstudent["lisi", 1]);
            //查找张三的成绩
            list<student> student = fstudent["zhangsan"];
            int result = 0;
            if (student.count > 0)
            {
                foreach (var s in student)
                {
                    console.writeline(string.format("张三 课程编号{0} 成绩为:{1}", s.courseid, s.score));
                    result += s.score;
                }
                console.writeline(string.format("张三的平均分为{0}:", result / 3));
            }
               
            else {
                console.writeline("查无此人");
            }



        }
    }   
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网