如何填充类中定义的数组元素?我想填充一系列Marks的学生名单,我无法找到。
public class Marks
{
public int ENG { get; set; }
public int MATHS { get; set; }
}
public class Student
{
public string empName { set; get; }
public string empAddress { set; get; }
public Marks[] StudentMarks { set; get; }
}
var objs = new List<Student>()
{
new Employee() {empName = "Manish" empAddress = "MUM"....array element with Marks of two subjects
new Employee() {empName = "Manoj", empAddress = "MUM"....arrayelement with Marks of two subjects
}
答案 0 :(得分:0)
为了在一行中初始化数组,您应该使用以下代码之一
int[] n1 = new int[4] {2, 4, 6, 8};
int[] n2 = new int[] {2, 4, 6, 8};
int[] n3 = {2, 4, 6, 8};
在您的情况下,您应该使用Marks对象填充数组。
Marks[] MarksArray = new [] {new Marks() {ENG = 1, MATHS = 1}, new Marks() {ENG = 2, MATHS = 2}};
此数组包含2个标记 - new Marks() {ENG = 1, MATHS = 1}
和new Marks() {ENG = 2, MATHS = 2}
。
您可以在此处阅读更多示例 - https://msdn.microsoft.com/en-us/library/aa287601(v=vs.71).aspx
所以你可以用这种方式创建你的对象
var objs = new List<Student>()
{
new Student() {empName = "Manish", empAddress = "MUM", StudentMarks = new [] {new Marks() {ENG = 1, MATHS = 1}, new Marks() {ENG = 2, MATHS = 2}} },
new Student() {empName = "Manish", empAddress = "MUM", StudentMarks = new [] {new Marks() {ENG = 1, MATHS = 1}, new Marks() {ENG = 2, MATHS = 2}} },
};
答案 1 :(得分:0)
如果您只关心语法,那么这样做:
void Main()
{
var objs = new List<Employee>()
{
new Employee() {
empName = "Manish",
empAddress = "MUM",
StudentMarks = new Marks[] {
new Marks {ENG=10, MATHS = 100},
new Marks { ENG=20, MATHS = 80}, }},
new Employee() {
empName = "Manoj",
empAddress = "MUM",
StudentMarks = new Marks[] {
new Marks { ENG=59, MATHS = 40},
new Marks { ENG=60, MATHS = 80},
new Marks { ENG=80, MATHS = 10},
new Marks { MATHS = 90},
new Marks { ENG=70},}
},
};
foreach (var student in objs)
{
Console.WriteLine("{0} - {1}", student.empName, student.empAddress);
foreach (var mark in student.StudentMarks)
{
Console.WriteLine("Eng:{0}, Maths:{1}", mark.ENG, mark.MATHS);
}
}
}
public class Marks
{
public int ENG { get; set; }
public int MATHS { get; set; }
}
public class Student
{
public string empName { set; get; }
public string empAddress { set; get; }
public Marks[] StudentMarks { set; get; }
}
public class Employee : Student
{ }
然而,这不是一个好模型。一个更好的模型是这样的:
void Main()
{
var objs = new List<Student>()
{
new Student() {
Name = "Manish",
Address = "MUM",
StudentMarks = new List<Mark> {
new Mark {Name="Eng", Score = 50},
new Mark {Name="Maths", Score = 60},
}
},
new Student() {
Name = "Manoj",
Address = "MUM",
StudentMarks = new List<Mark> {
new Mark {Name="Maths", Score = 100},
new Mark {Name="Eng", Score = 80},
new Mark {Name="Eng", Score = 70},
new Mark {Name="Eng", Score = 90},
new Mark {Name="Maths", Score = 90},
}
},
};
foreach (var student in objs)
{
Console.WriteLine("{0} - {1}", student.Name, student.Address);
foreach (var mark in student.StudentMarks)
{
Console.WriteLine("Name:{0}, Score:{1}", mark.Name, mark.Score);
}
}
}
public class Mark
{
public string Name { get; set; }
public int Score { get; set; }
}
public class Student
{
public string Name { set; get; }
public string Address { set; get; }
public List<Mark> StudentMarks { set; get; }
}
这个还不是很好。一个更好的可能是:
void Main()
{
var objs = new List<Student>()
{
new Student() {
Name = "Manish",
Address = "MUM",
StudentMarks = new Dictionary<string,List<int>> {
{"Maths", new List<int> {60,70,50}},
{"Eng", new List<int> {80,70,90}},
}
},
new Student() {
Name = "Manoj",
Address = "MUM",
StudentMarks = new Dictionary<string,List<int>> {
{"Maths", new List<int> {70,90}},
{"Eng", new List<int> {40,50,60,60}},
}
},
};
foreach (var student in objs)
{
Console.WriteLine("{0} - {1}", student.Name, student.Address);
foreach (var course in student.StudentMarks)
{
Console.WriteLine("Course:{0}, Average:{1}", course.Key, course.Value.Average());
foreach (var score in course.Value)
{
Console.WriteLine(score);
}
}
}
}
public class Mark
{
public string Name { get; set; }
public int Score { get; set; }
}
public class Student
{
public string Name { set; get; }
public string Address { set; get; }
public Dictionary<string,List<int>> StudentMarks { set; get; }
}
还有更好的。