求一个c#代码,要求如下

1、定义一个学生类Student,含有学号、姓名和成绩三个属性。

2、定义一个学生管理类Student Manager,在该类中定义索引器按照学号访问学生信息。该类的基本框架如下:

class StudentManager{

//学生列表

List lstStudent;

//构造函数

//索引器的定义

//输入学生信息方法

}

3、在Main方法中创建StudentManager对象,调用其中定义的方法输入数据,并利用索引器查找指定学号的学生信息,然后输出。

回答:有点难呐,不熟悉ArrayList ,只能够写出来,遇到了问题,那就是在 ArrayList里面存放的数据格式是啥样的,如何取出其中某一个字段(就是一次取三个,刚好是Student的三个属性),于是代码长这样,功能并达不到要求,代码如下:
Student

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    public class Student
    {
        public string id
        {
            get => default;
            set
            {
            }
        }

        public string name
        {
            get => default;
            set
            {
            }
        }

        public string score
        {
            get => default;
            set
            {
            }
        }
    }
}

StudentManager

using System;
using System.Collections;

namespace Test
{
    public class StudentManager : Student
    {
        public void Add(ArrayList list)     //id , name , score
        {           
            Student student = new Student();
            Console.WriteLine("请输入学生学号、姓名、成绩:");

            string id = Console.ReadLine();
            string name = Console.ReadLine();
            string score = Console.ReadLine();

            student.id = id;
            student.name = name;
            student.score = score;
            list.Add(student);
        }

        public void Search(ArrayList list)
        {
            Console.WriteLine("请输入你要搜索的学号:");
            string id = Console.ReadLine();
            bool n = false;

            for(int i = 0; i < list.Count; i++)
            {
                Student student = (Student)list[i];
                if (id == student.id)
                {
                    Console.WriteLine("学号:",student.id);
                    Console.WriteLine("姓名:", student.name);
                    Console.WriteLine("成绩:", student.score);
                    n = true;
                }              
            }
            if (!n)
            {
                Console.WriteLine("未找到该学号");
            }
        }
    }
}

主函数

using System;
using System.Collections;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();

            StudentManager manager = new StudentManager();

            while (true)
            {
                Console.WriteLine("成绩系统,请选择功能:(1、添加学生  2、查找并显示学生):");

                int n = int.Parse(Console.ReadLine());

                switch (n)
                {
                    case 1:
                        manager.Add(list);
                        Console.WriteLine("添加完毕,已退出");
                        Console.WriteLine();
                        break;
                    case 2:
                        manager.Search(list);
                        Console.WriteLine("查找完毕。已退出");
                        Console.WriteLine();
                        break;
                    default:
                        Console.WriteLine("输入有误,已退出");
                        Console.WriteLine();
                        break;
                }               
            }
        }
    }
}

代码截图,三个文件哈,没放在一起,放在一起会更好,反正也小:

img

生成类图:

img

运行截图,报错了呀:

img


using System;
using System.Collections.Generic;

class student
{
    
    string _id;
    string _name;
    int _grade;
    

    public student()
    {

    }
    public student(string id, String name, int grade)
    {
        this._id = id;
        this._name= name;
        this._grade = grade;
    }
    public string Id
    {
        get { return _id; }
        set { _id = value; }
    }
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    public int Grade
    {
        get { return _grade; }
        set {
            if (value >= 0 && value <= 100)
                _grade = value;
            else
            {
                Console.WriteLine("数据不符合要求!");
                return;
            }        
            }
    }
}
class StudentManager
{
    List<student> IstStudent=new List<student>();
    public StudentManager() 
    {
        
    }
    public student this[string id]
    {
        get
        {          
            foreach(student next in IstStudent)
                if(next.Id == id)
                {
                    return next;
                }
                    
            return null;
        }      
    }
    public void insert(string id,string name,int grade)
    {
        student p1 = new student(id, name, grade);
        IstStudent.Add(p1);
    }

    public static implicit operator StudentManager(string v)
    {
        throw new NotImplementedException();
    }
}
namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            student student = new student();
            StudentManager stu = new StudentManager();
            Console.WriteLine("50012001");
            for (int i = 0; i < 3; i++)
            {
                string a = Console.ReadLine();
                string []info=a.Split(" ");
                string a1="", a2=""; int score = 0;
                a1 = info[0]; a2 = info[1]; score = Convert.ToInt32(info[2]);
                stu.insert(a1,a2,score);
            }
            string s = Console.ReadLine();
            student stu1 = stu[s];
            Console.WriteLine(stu1.Id+" "+stu1.Name+" "+stu1.Grade);
        }
    }
}