Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何创建学生和成绩的通用列表并访问这些列表_C#_Generics - Fatal编程技术网

C#如何创建学生和成绩的通用列表并访问这些列表

C#如何创建学生和成绩的通用列表并访问这些列表,c#,generics,C#,Generics,我正在与C#做一些本应该很容易的事情。我需要一个临时存储系统,用于未知数量的学生和未知数量的每个学生的属性 我基本上得到了一个未知数量的学生,然后对每个学生进行查询,返回他们的成绩和其他信息,这些信息可能与其他学生不同 学生1: 姓名:约翰 姓氏:Doe 数学1010:A 数学2020:B 数学3010:B+ 英语1010:A- 学生2: 姓名:April 姓:约翰逊 地质学1000:C 数学1010:B 等等 最后,我只需要逐级检查每个学生并输出他们的信息 我发现这个例子适合每个学生一组已

我正在与C#做一些本应该很容易的事情。我需要一个临时存储系统,用于未知数量的学生和未知数量的每个学生的属性

我基本上得到了一个未知数量的学生,然后对每个学生进行查询,返回他们的成绩和其他信息,这些信息可能与其他学生不同

  • 学生1: 姓名:约翰 姓氏:Doe 数学1010:A 数学2020:B 数学3010:B+ 英语1010:A-

  • 学生2: 姓名:April 姓:约翰逊 地质学1000:C 数学1010:B 等等

最后,我只需要逐级检查每个学生并输出他们的信息

我发现这个例子适合每个学生一组已知的项目,但我想我需要每个学生的一个列表,我不知道如何制作“主”列表。我可以为数组找到它,但使用泛型对我来说是新的

List<Student> lstStudents = new List<Student>();

Student objStudent = new Student();
objStudent.Name = "Rajat";
objStudent.RollNo = 1;

lstStudents.Add(objStudent);

objStudent = new Student();
objStudent.Name = "Sam";
objStudent.RollNo = 2;

lstStudents.Add(objStudent);

//Looping through the list of students
foreach (Student currentSt in lstStudents)
{
    //no need to type cast since compiler already knows that everything inside 
    //this list is a Student
    Console.WriteLine("Roll # " + currentSt.RollNo + " " + currentSt.Name);
}
List lststudens=newlist();
学生对象学生=新学生();
objStudent.Name=“Rajat”;
objStudent.RollNo=1;
lststudens.Add(objStudent);
objStudent=新学生();
objStudent.Name=“Sam”;
objStudent.RollNo=2;
lststudens.Add(objStudent);
//循环浏览学生名单
foreach(学生当前的学生人数)
{
//不需要输入cast,因为编译器已经知道其中的所有内容
//这个名单是一个学生
Console.WriteLine(“Roll#“+currentSt.RollNo+”+currentSt.Name);
}

您的学生需要一个字段

class Student
{
    public Dictionary<string, object> Attributes = new Dictionary<string, object>();
}
当然,您也可以与固定属性混合使用。
要实现良好的编码,您应该使用属性和助手成员函数来实现。我的示例非常基本。

您可以声明一个学生类,如:

    public class Student
    {
        private readonly Dictionary<string, object> _customProperties = new Dictionary<string, object>();

        public Dictionary<string, object> CustomProperties { get { return _customProperties; } }
    }
公共班级学生
{
专用只读词典_customProperties=new Dictionary();
公共字典CustomProperties{get{return}\u CustomProperties;}}
}
然后像这样使用它:

        List<Student> lstStudents = new List<Student>();

        Student objStudent = new Student();
        objStudent.CustomProperties.Add("Name", "Rajat");
        objStudent.CustomProperties.Add("RollNo", 1);

        lstStudents.Add(objStudent);

        objStudent = new Student();
        objStudent.CustomProperties.Add("Name", "Sam");
        objStudent.CustomProperties.Add("RollNo", 2);

        lstStudents.Add(objStudent);

        foreach (Student currentSt in lstStudents)
        {
            foreach (var prop in currentSt.CustomProperties)
            {
                Console.WriteLine(prop.Key+" " + prop.Value);
            }

        }
List lststudens=newlist();
学生对象学生=新学生();
objStudent.CustomProperties.Add(“Name”、“Rajat”);
添加(“RollNo”,1);
lststudens.Add(objStudent);
objStudent=新学生();
添加(“名称”、“Sam”);
添加(“RollNo”,2);
lststudens.Add(objStudent);
foreach(学生当前的学生人数)
{
foreach(currentSt.CustomProperties中的var属性)
{
控制台写入线(属性键+“”+属性值);
}
}

很好。你的问题到底是什么?这个问题有点含糊不清-你是否将属性保存在
学生
类中(在这种情况下,每个学生都会有一个
列表
字典
)或者你想要某种
字典
学生
对象映射到属性吗?那不应该是
foreach
而不是
for
?你是对的。谢谢我混淆了一些objc-js语法。
        List<Student> lstStudents = new List<Student>();

        Student objStudent = new Student();
        objStudent.CustomProperties.Add("Name", "Rajat");
        objStudent.CustomProperties.Add("RollNo", 1);

        lstStudents.Add(objStudent);

        objStudent = new Student();
        objStudent.CustomProperties.Add("Name", "Sam");
        objStudent.CustomProperties.Add("RollNo", 2);

        lstStudents.Add(objStudent);

        foreach (Student currentSt in lstStudents)
        {
            foreach (var prop in currentSt.CustomProperties)
            {
                Console.WriteLine(prop.Key+" " + prop.Value);
            }

        }