title: c# - How to save List of objects into a text file
body: <p>I have a simple windows form application. Basically, I want to save the user's input of their ID number and name. I have a Person class:</p>
  
  <pre><code>class Person
  {
      private string nric;
      private string name;
  
      public Person(string nric, string name)
      {
          this.nric = nric;
          this.name = name;
      }
  </code></pre>
  
  <p>And the .cs file:</p>
  
  <pre><code>    private void save_Click(object sender, EventArgs e)
      {
          List&lt;Person&gt; pList = new List&lt;Person&gt;();
          Person p1 = new Person(nric.Text, name.Text);
          pList.Add(p1);
  
          using (TextWriter tw = new StreamWriter("PersonFile.txt"))
          {
              foreach (Person p in pList)
              {
                  tw.WriteLine(p);
              }
              tw.Close();
          }
      }
  </code></pre>
  
  <p>When I tried to run the codes, this is the output shown in the PersonFile.txt:</p>
  
  <pre><code>   Testing.Person
  </code></pre>
  
  <p>How do I make it such that the ID number and name would be displayed in the text file?</p>