语法虚拟成员 [英] Syntax for virtual members
本文介绍了语法虚拟成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑写在C#.NET 4.0(通常在NHibernate的类中找到)以下类:
Consider the following class written in c# .net 4.0 (typically found in a nhibernate class):
public class CandidateEntity : EntityBase
{
public virtual IList<GradeEntity> Grades { get; set; }
public CandidateEntity()
{
Grades = new List<GradeEntity>();
}
}
这行得到了充分的理由警告虚成员通话在构造函数。
我应该在哪里初始化这个集合?
This line gets a well founded warning "virtual member call in the constructor". Where shall I initialize this collection ?
问候,
推荐答案
支持字段是一种方法。另一种方法是使用一个专用的制定者。这工作得很好NHibernate的
The backing field is one way. Another is to use a private setter. This works well in nHibernate.
public virtual IList<GradeEntity> Grades { get; private set; }
public CandidateEntity()
{
Grades = new List<GradeEntity>();
}
这篇关于语法虚拟成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文