title: C# binary search tree
body: <p>I was making a test case for some code on binary search tree my professor gave</p>
  
  <pre><code>public static void Main(string [] args)
  {
      //on my prof's code, public class BinSearchTree&lt;T&gt;
      BinSearchTree&lt;int&gt; myTree = new BinSearchTree&lt;int&gt;();
  
      myTree.Insert(10);
      myTree.Insert(15);
      myTree.Insert(5);
      myTree.Insert(2);
      myTree.Insert(1);
  
      Console.WriteLine(myTree.ToString());
      Console.ReadKey();
  }
  </code></pre>
  
  <p>It compiles, but it displays</p>
  
  <p><code>BinSearchTree`1[System.Int32]</code></p>
  
  <p>Can somebody tell me why it displays that?</p>
  
  <p>my prof's code:</p>
  
  <pre><code>public class BinSearchTree&lt;T&gt; where T : IComparable&lt;T&gt;
  {
  private class OurTreeNode&lt;T&gt;
  {
      public T Data { get; set; }
      public OurTreeNode&lt;T&gt; Left;
      public OurTreeNode&lt;T&gt; Right;
      public OurTreeNode(T d = default(T), OurTreeNode&lt;T&gt; leftnode = null, OurTreeNode&lt;T&gt; rightnode = null)
      {
          Data = d;
          Left = leftnode;
          Right = rightnode;
      }
  
      public override string ToString()
      {
          return Data.ToString();
      }
  }
  //...other methods
  
  //prof's Insert method
  public void Insert(T newItem)
  {
      mRoot = Insert(newItem, mRoot);
  }
  private OurTreeNode&lt;T&gt; Insert(T newItem, OurTreeNode&lt;T&gt; pTmp)
  {
      if (pTmp == null)
          return new OurTreeNode&lt;T&gt;(newItem, null, null);
      else if (newItem.CompareTo(pTmp.Data) &lt; 0)
          pTmp.Left = Insert(newItem, pTmp.Left);
      else if (newItem.CompareTo(pTmp.Data) &gt; 0)
          pTmp.Right = Insert(newItem, pTmp.Right);
      else
          throw new ApplicationException("...");
  
      return pTmp;
  }
  }
  </code></pre>
  
  <p>I tried adding a ToString() method after the Insert method but it gives me an error when I used foreach. Is there a way of displaying it without making too much extra methods? </p>