namespace TestOOP
{
using System;
using System.Collections.Generic;
using System.Linq;
internal sealed class Student
{
private string name;
}
internal sealed class Course
{
private ICollection<Student> students;
public ICollection<Student> Students
{
get { return this.students; }
set { this.students = Students; }
}
}
class Program
{
static void Main()
{
var course = new Course();
course.Students.Add(new Student());
Console.WriteLine(course.Students.Count());
}
}
}
那就是我的代码。在运行它时,我没有将对象设置为我尝试将学生添加到课程的对象的实例。我需要帮助解释如何使用接口作为字段。如何将界面用作字段?
具体来说,在这种情况下,您不要将值设置为'学生'属性(或后场'学生'),所以它是'null'并且会抛出'Students.Count()'。 – Sinatr