如何实现接口属性?

haa*_*nsi -1 .net c# nullreferenceexception

以下是我的界面:

public interface IBaseService
{
    List<ExceptionPairs> Exceptions { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

另一个接口是继承它:

public interface IClassStudentsService: IBaseService
{

}
Run Code Online (Sandbox Code Playgroud)

我在下面的类中实现了这个接口:

public class CSService : IClassStudentsService
{
    public List<ExceptionPairs> Exceptions
    {
        get;set;
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个CSService对象,并尝试访问List 'Exceptions'但收到错误" Object reference not set to an instance of an object."

你能指导我需要做什么实例化吗?

Mat*_*ter 5

Exceptions 是对象的自动属性,因此在访问它之前它尚未初始化.

在构造函数中,使用新列表初始化该属性:

public class CSService : IClassStudentsService
{
    public CSService() {
         Exceptions = new List<ExceptionPairs>();
    }

    public List<ExceptionPairs> Exceptions { get; set; }
}
Run Code Online (Sandbox Code Playgroud)