当类的一个属性是列表时,如何打印类的成员
本文关键字:何打印 打印 成员 属性 一个 列表 | 更新日期: 2023-09-27 17:53:55
我有一个类,其中一个属性是列表:
public class Course
{
public int CourseId { get; set; }
public string Name { get; set; }
public List<Student> Students { get; set; }
}
我想打印出每个Course ID
和Name
课程的学生名单。
我有一种方法可以打印出各种CourseIds
和Course
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);
}
但是我不知道如何遍历每门课程的学生并打印他们的详细信息。
使用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;
}
如果您使用自定义方法来获取要显示的字符串,我将对每个部分使用类似的方法。
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);
}
}
为了保持简洁,在您的Student
类中添加一个override ToString()
,它返回Student
信息,与FormatDetailsForDisplay()
方法显示Class
信息的方式非常相似。override 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
foreach (Course course in courses)
{
resultLabel.Text += course.FormatDetailsForDisplay();
foreach(var student in course.Students)
resultLabel.Text += "student:" + student.Name;
}