当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Turn model data into string

问题描述:

This question already has an answer here:

  • What is the best way to dump entire objects to a log in C#?

    11 answers

网友答案:

This is one of the reason behind the ToString method being overridable.
You can simply override the ToString() inside your class' model and return whatever you like

public class myModel
{ 
   public string Item1 {get;set;}
   public string Item2 {get;set;}
   public string Item3 {get;set;}

   public override string ToString()
   {
       return string.Join(" ", this.Item1, this.Item2, this.Item3);
   }
}

....

myModel m = new myModel()
{
    Item1 = "This is",
    Item2 = "a test",
    Item3 = "to make one sentence"
};

Console.WriteLine(m.ToString());
分享给朋友:
您可能感兴趣的文章:
随机阅读: