我有一个如下的类:
问题在于当我创建一个
public class HintQuestion
{
public string QuestionCode { get; set; }
public string QuestionName { get; set; }
}
还有另一个类如下所示
public class User
{
public User()
{
HintQuestion = new HintQuestion();
}
public HintQuestion HintQuestion { get; set; }
}
问题在于当我创建一个
User
类的实例并给内部类对象赋值时,它不起作用。我正在使用对象初始化。User u=new user{HintQuestion.QuestionCode ="",...
但是当我创建构造函数时,它能正常工作。
User u = new user { HintQuestion = new HintQuestion { QuestionCode = "", QuestionName = "" } };
或User u = new user { HintQuestion = new HintQuestion() };
。你也可以尝试User u = new User { HintQuestion.QuestionCode = "", };
,但我不知道是否会出现NRE。顺便说一下,与类名相同的变量名会令人困惑。 - Timnew User { HintQuestion.QuestionCode=""
时,是否出现了错误?如果有,错误信息是什么?还是没有问题? - Tim