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

后端 未结 5 1185
没有蜡笔的小新
没有蜡笔的小新 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:53

    If that Question is a class, then you could shorten the code a bit:

        var q = itemView.Question;
        q.AnswersJSON = itemView.Answer.ToJSONString();
        q.Modified = DateTime.Now;
        q.ModifiedBy = User.Identity.Name
    

提交回复
热议问题