如何在类的某个属性是列表时打印类的成员

时间:2015-06-06 01:05:01

标签: c#

我有一个类,其中一个属性是列表:

public class Course
{
    public int CourseId { get; set; }
    public string Name { get; set; }
    public List<Student> Students { get; set; }
}

我想为每个Course ID和课程Name打印出注册课程的学生名单。

我有办法打印各种CourseIdsCourse Name,即:

foreach (Course course in courses)
{
    resultLabel.Text += course.FormatDetailsForDisplay();

其中:

public string FormatDetailsForDisplay()
{
    return String.Format("Course ID: {0} - Name: {1} <br/>", this.CourseId, this.Name);
}

但我不知道如何为每门课程迭代学生并打印他们的详细信息。

4 个答案:

答案 0 :(得分:2)

使用ForEach

courses.ForEach(FormatDetailsForDisplay);

public void FormatDetailsForDisplay(Course course)
{
   string f = String.Format("Course ID: {0} - Course Name: {1} ", course.CourseId, course.Name);
   foreach (var item in course.Students)
   {
     f += "Student Name:" + item.Name;
   }
     resultLabel.Text += f;
}

答案 1 :(得分:0)

如果您使用自定义方法来显示字符串,我会对每个部分使用类似的方法。

public string FormatDetailsForDisplay()
{
    StringBuilder sb = new StringBuilder();
    sb.Append("Course ID: ").Append(this.CourseId);
    sb.Append(" - Name: ").Append(this.Name).Append(" - Students: {");
    sb.Append(this.Students[0].FormatDetailsForDisplay());
    for (int i = 1; i < this.Students.Count; i++)
    {
        sb.Append(", ").Append(this.Students[i].FormatDetailsForDisplay());
    }
    sb.Append("} <br/>");
    return sb.ToString();
}
// Update FormatDetailsForDisplay() for the Students class depending on how you built it
public class Student
{
    public string Name;
    public int Age;

    public string FormatDetailsForDisplay()
    {
        return String.Format("Name: {0} - Age: {1}", this.Name, this.Age);
    }
}

答案 2 :(得分:0)

要保持简短和甜蜜,请在override ToString()类中添加Student,该类返回Student信息,与FormatDetailsForDisplay()方法显示有关信息的方式非常相似Classoverride ToString看起来像是:

public override string ToString()
{
    // Add what ever properties you have
    // I just used ID and Name
    return String.Format("\tID: {0} - Name: {1}\r\n", ID, Name);
}

现在,在FormatDetailsForDisplay()您可以执行以下操作:

public string FormatDetailsForDisplay()
{
    return String.Format("Course ID: {0} - Name: {1}\r\n{2}", 
                         this.CourseId, 
                         this.Name,
                         String.Join("", this.Students.Select(s => s)));
}

将所有内容放在一起,你就会有:

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

public class Program
{
    public static void Main()
    {
        Course course = new Course {
            CourseId = 123456,
            Name = "Math",
            Students = new List<Student>()
        };

        course.Students.Add(new Student() {
            ID = 11111,
            Name = "John Doe"
        });
        course.Students.Add(new Student() {
            ID = 22222,
            Name = "Jane Doe"
        });

        Console.WriteLine(course.FormatDetailsForDisplay());
    }
}

public class Course
{
    public int CourseId { get; set; }
    public string Name { get; set; }
    public List<Student> Students { get; set; }

    public string FormatDetailsForDisplay()
    {
        return String.Format("Course ID: {0} - Name: {1}\r\n{2}", 
                             this.CourseId, 
                             this.Name,
                             String.Join("", this.Students.Select(s => s)));
    }
}

public class Student
{
    public int ID { get; set; }
    public string Name { get; set; }

    public override string ToString()
    {
        return String.Format("\tID: {0} - Name: {1}\r\n", ID, Name);
    }
}

结果:

Course ID: 123456 - Name: Math
    ID: 11111 - Name: John Doe
    ID: 22222 - Name: Jane Doe

请参阅此处的工作示例... https://dotnetfiddle.net/arFF4t

答案 3 :(得分:-3)

foreach (Course course in courses) 
{
  resultLabel.Text += course.FormatDetailsForDisplay();
  foreach(var student in course.Students)
   resultLabel.Text += "student:" + student.Name;
}