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());
}
}
}
これは私のコードです。それを実行すると、私はコースに学生を追加しようとする行のオブジェクトのインスタンスに設定されていないオブジェクトを取得します。インターフェースをフィールドとして使う方法を説明する助けが必要です。コレクションの施設interfaceをフィールドとして使用する方法は?
この場合、値は「Students」プロパティ(またはバックフィールド「students」)に設定されないため、「null」であり、「Students.Count()」がスローされます。 – Sinatr