You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中调用方法显示两个学生列表并修复CSV读取问题?

解决CSV读取学生列表无法显示的问题

问题根源

你的代码存在几个关键错误,导致studentList2无法正常工作:

  • 提前尝试添加未定义的s6、s7到studentList2,会直接触发编译报错
  • CSV读取循环中创建的Student对象未添加到studentList2
  • Student类没有适配字符串参数的构造函数,直接用CSV字符串创建对象会失败
  • CSV中的日期格式需要显式转换为DateTime类型

修复步骤

1. 给Student类添加适配CSV的构造函数

在Student类中新增构造函数,接收字符串参数并完成类型转换:

// 新增适配CSV的构造函数
public Student(string idStr, string? n, string? t, string dobStr)
{
    ID = int.Parse(idStr);
    Name = n;
    Tel = t;
    // 解析CSV中的dd/MM/yyyy格式日期
    DateOfBirth = DateTime.ParseExact(dobStr, "dd/MM/yyyy", null);
}

2. 修正studentList2的初始化与CSV读取逻辑

删除提前添加s6、s7的错误代码,在CSV读取循环中创建Student对象并添加到列表:

// 初始化空列表,不要提前添加未定义对象
List<Student> studentList2 = new List<Student>();

string[] csvLines = File.ReadAllLines("Students.csv");
// 跳过表头,从第2行开始读取数据
for (int i = 1; i < csvLines.Length; i++)
{
    string[] data = csvLines[i].Split(',');
    // 创建Student对象并添加到列表
    Student studentFromCsv = new Student(data[0], data[1], data[2], data[3]);
    studentList2.Add(studentFromCsv);
}

3. 调整代码执行顺序

确保在读取CSV并填充studentList2之后,再调用DisplayOutput(studentList2),否则会显示空列表。

4. 可选:合并两个学生列表并显示

如果需要将studentList和studentList2合并显示,使用AddRange方法:

List<Student> combinedList = new List<Student>();
combinedList.AddRange(studentList);
combinedList.AddRange(studentList2);
DisplayOutput(combinedList);

完整修改后的代码

Student类完整代码

using System;

namespace school
{
    class Student
    {
        private int id;
        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        private string? name;
        public string? Name
        {
            get { return name; }
            set { name = value; }
        }

        private string? tel;
        public string? Tel
        {
            get { return tel; }
            set { tel = value; }
        }

        private DateTime dateOfBirth;
        public DateTime DateOfBirth
        {
            get { return dateOfBirth; }
            set { dateOfBirth = value; }
        }

        // 原有构造函数
        public Student(int d, string? n, string? t, DateTime dob)
        {
            ID = d;
            Name = n;
            Tel = t;
            DateOfBirth = dob;
        }

        // 新增适配CSV的构造函数
        public Student(string idStr, string? n, string? t, string dobStr)
        {
            ID = int.Parse(idStr);
            Name = n;
            Tel = t;
            DateOfBirth = DateTime.ParseExact(dobStr, "dd/MM/yyyy", null);
        }

        public string StudentFullName()
        {
            return ID + " " + Name + " " + Tel + " " + DateOfBirth;
        }
    }
}

主程序完整代码

using school;
using System;
using System.Collections.Generic;
using System.IO;

namespace YourNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dob1 = new DateTime(2000, 10, 13);
            Student s1 = new Student(1, "John Tan", "88552211", dob1);
            DateTime dob2 = new DateTime(2001, 11, 01);
            Student s2 = new Student(2, "Peter Lim", "85678141", dob2);
            DateTime dob3 = new DateTime(2000, 01, 03);
            Student s3 = new Student(3, "David Chan", "88555461", dob3);
            DateTime dob4 = new DateTime(2000, 05, 07);
            Student s4 = new Student(4, "Muhammed Faizal", "98762211", dob4);
            DateTime dob5 = new DateTime(2000, 08, 09);
            Student s5 = new Student(5, "Esther Eng", "83352245", dob5);

            List<Student> studentList = new List<Student>();
            studentList.Add(s1);
            studentList.Add(s2);
            studentList.Add(s3);
            studentList.Add(s4);
            studentList.Add(s5);

            DisplayOutput(studentList);

            studentList.Add(GetStudent());
            DisplayOutput(studentList);

            // 处理CSV数据
            List<Student> studentList2 = new List<Student>();
            string[] csvLines = File.ReadAllLines("Students.csv");
            for (int i = 1; i < csvLines.Length; i++)
            {
                string[] data = csvLines[i].Split(',');
                Student studentFromCsv = new Student(data[0], data[1], data[2], data[3]);
                studentList2.Add(studentFromCsv);
            }

            // 显示CSV读取的学生列表
            DisplayOutput(studentList2);

            // 可选:显示合并后的列表
            List<Student> combinedList = new List<Student>();
            combinedList.AddRange(studentList);
            combinedList.AddRange(studentList2);
            Console.WriteLine("\n=== 合并后的所有学生 ===");
            DisplayOutput(combinedList);
        }

        static void DisplayOutput(List<Student> sList)
        {
            Console.WriteLine("\n{0,-5} {1,-17} {2,-10} {3,-20}",
                "ID", "Name", "Tel", "Date of Birth");

            foreach (Student s in sList)
            {
                Console.WriteLine("{0,-5} {1,-17} {2,-10} {3,-20}",
                   s.ID, s.Name, s.Tel, s.DateOfBirth.ToString("dd/MM/yyyy"));
            }
        }

        static Student GetStudent()
        {
            Console.Write("Enter id: ");
            int id = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter name: ");
            string? name = Console.ReadLine();

            Console.Write("Enter tel: ");
            string? tel = Console.ReadLine();

            Console.Write("Enter Date of Birth (dd/MM/yyyy): ");
            DateTime dateOfBirth = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy", null);

            Console.WriteLine("");

            return new Student(id, name, tel, dateOfBirth);
        }
    }
}

内容的提问来源于stack exchange,提问作者zijcxlkcj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 15:05:36