在 C# 中使用继承时无法打印数组的值

Not able to print value of an array while using inheritance in C#

我正尝试在使用继承和多态性的练习中从数组中打印...等等

练习是一个学生和老师class,它们继承自一个人class,但通过重写两个class 中的打印方法显示不同的结果。教师 class 从字符串变量打印,而学生 class 从字符串变量和字符串数组打印。我能够打印除打印字符串数组的部分之外的所有内容,我得到 System.String[] 我的理解是它打印对象的名称而不是值。我尝试重写 To.String 方法以将其传递给 printDetails 方法,但由于我的 printDetails 方法有一个 return 类型的 void,我不能那样做。

下面是我的代码供参考:

Program.cs :


namespace Task3
{
    class Program
    {
        static void Main(string[] args)
        {
           
            var people = new List<Person>();

            string[] subjects = { "Math", "Science", "English" };
            string studentName = "Sue";

            Student student = new Student(studentName, subjects);
            people.Add(student);
         
            
            string faculty = "Computer Science";
            string teacherName = "Tim";

            Teacher teacher = new Teacher(teacherName, faculty);
            people.Add(teacher);


            foreach (var element in people)
            {
                element.PrintDetails();
            }

            Console.ReadKey(); 
        }
    }
}

Person.cs :

namespace Task3
{
    public abstract class Person
    {
        protected string _name;
        public Person(){}

        public Person(string name)
        {
            _name = name;
        }

        public abstract void PrintDetails();
    }
}

Student.cs :

namespace Task3
{
    public class Student : Person
    {
        private string[] _subjects;

        public Student(string name, string[] subjects) : base(name)
        {
            
            _subjects = subjects;
        }

        public override void PrintDetails()
        {
            Console.WriteLine("Hi my name is " + _name + " and I am studying " + _subjects);        
        }        
    }
}

Teacher.cs :

namespace Task3
{
    public class Teacher : Person
    {
        private string _faculty;

        public Teacher(string name, string faculty) : base(name)
        {

            _faculty = faculty;
        }

        public override void PrintDetails()
        {
            Console.WriteLine($"Hi my name is {_name} and I teach in the {_faculty} faculty");
        }
    }
}

下面是我得到的输出和我应该得​​到的输出:

您的 PrintDetails() 方法打印的数组没有任何格式(因此它只打印数组类型)

使用string.Join打印逗号分隔的数组

public override void PrintDetails()
{
   Console.WriteLine("Hi my name is " + _name + " and I am studying " + string.Join(",", _subjects));        
} 

应该这样做。