我有一种获取学生数据的方法:
const G = [
[9,9,9,9,9,9]
]
const P = [
[9,9]
]
let availableFirstLines = [];
G.forEach((Gv, Gi) => {
const Gs = Gv.toString();
P.forEach(Pv => {
const Ps = Pv.toString();
let i, fi = 0;
while ((i = Gs.indexOf(Ps, fi)) >= 0) {
availableFirstLines.push({
line: Gi,
charAtStringStart: i,
charAtStringEnd: i + Ps.length - 1
});
fi = i + 1;
}
})
});
console.log(availableFirstLines);
我正在尝试创建一种使用 public static List<Student> getStudentInfo()
{
List<Student> students = new List<Student>();
students.Add(new Student(1, "Doe", "John", "someData"));
students.Add(new Student(2, "Pit", "Brat", "someData1"));
students.Add(new Student(5, "Kardasian", "Kim", "someData2"));
students.Add(new Student(4, "Lee", "Chun", "someData"));
.
.
return students;
}
foreach (Student s in students)
Console.WriteLine(s);
来检索上述数据的方法
,但我无法获取并打印数据。
如何使用方法访问列表?
答案 0 :(得分:6)
ToString()
请参阅How to: Override the ToString Method (C# Programming Guide)
class Person { public string Name { get; set; } public int Age { get; set; } public override string ToString() { return "Person: " + Name + " " + Age; } }
foreach (Student s in students)
Console.WriteLine($"{s.Id} {s.SomeData}");
var students = getStudentInfo();
foreach (Student s in students)
Console.WriteLine(s); // `Student` doesn't override ToString(), this will be the name of the class.
foreach (Student s in students)
Console.WriteLine($"{s.Id}, {s.Name}");
输出
Program+Student
Program+Student
Program+Student
Program+Student
1, Doe
2, Pit
5, Kardasian
4, Lee