How can I simplify C# code that sets multiple properties of an object?

后端 未结 5 1182
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 00:30

I have code that looks like this:

itemView.Question.AnswersJSON = itemView.Answer.ToJSONString();
itemView.Question.Modified = DateTime.Now;
itemView.Question.Mo         


        
5条回答
  •  生来不讨喜
    2021-02-07 00:58

    Do you instantiate itemView.Question as part of your method?

    If so you could do:-

    itemView.Question = new ItemViewQuestion()
    {
      AnswersJSON = itemView.Answer.ToJSONString(),
      Modified = DateTime.Now,
      ModifiedBy = User.Identity.Name
    };
    

提交回复
热议问题