title: How do I simulate anonymous classes in C#
body: <p>I'm writing a small data structures library in C#, and I'm running into an architectural problem. Essentially I have a class which implements the visitor pattern, and there are many possible implementations of visitors:</p>
  
  <pre><code>public interface ITreeVisitor&lt;T, U&gt;
  {
      U Visit(Nil&lt;T&gt; s);
      U Visit(Node&lt;T&gt; s);
  }
  
  public abstract class Tree&lt;T&gt; : IEnumerable&lt;T&gt;
  {
      public readonly static Tree&lt;T&gt; empty = new Nil&lt;T&gt;();
      public abstract U Accept&lt;U&gt;(ITreeVisitor&lt;T, U&gt; visitor);
  }
  
  public sealed class Nil&lt;T&gt; : Tree&lt;T&gt;
  {
      public override U Accept&lt;U&gt;(ITreeVisitor&lt;T, U&gt; visitor) { return visitor.Visit(this); }
  }
  
  public sealed class Node&lt;T&gt; : Tree&lt;T&gt;
  {
      public Tree&lt;T&gt; Left { get; set; }
      public T Value { get; set; }
      public Tree&lt;T&gt; Right { get; set; }
  
      public override U Accept&lt;U&gt;(ITreeVisitor&lt;T, U&gt; visitor) { return visitor.Visit(this); }
  }
  </code></pre>
  
  <p>Anytime I want to pass in a visitor, I have to create a visitor class, implement the interface, and pass it in like this:</p>
  
  <pre><code>class InsertVisitor&lt;T&gt; : ITreeVisitor&lt;T, Tree&lt;T&gt;&gt; where T : IComparable&lt;T&gt;
  {
      public T v { get; set; };
  
      public Tree&lt;T&gt; Visit(Nil&lt;T&gt; s)
      {
          return new Node&lt;T&gt;() { Left = Tree&lt;T&gt;.empty, Value = v, Right = Tree&lt;T&gt;.empty };
      }
  
      public Tree&lt;T&gt; Visit(Node&lt;T&gt; s)
      {
          switch (v.CompareTo(s.Value))
          {
              case -1: return new Node&lt;T&gt;() { Left = Insert(v, s.Left), Value = s.Value, Right = s.Right };
              case 1: return new Node&lt;T&gt;() { Left = s.Left, Value = s.Value, Right = Insert(v, s.Right) };
              default: return s;
          }
      }
  }
  
  public static Tree&lt;T&gt; Insert&lt;T&gt;(T value, Tree&lt;T&gt; tree) where T : IComparable&lt;T&gt;
  {
      return tree.Accept&lt;Tree&lt;T&gt;&gt;(new InsertVisitor&lt;T&gt;() { v = value });
  }
  </code></pre>
  
  <p>I don't like writing that much boilerplate code, because it gets very messy when you have a non-trivial number of visitor implementations.</p>
  
  <p>I want to write something similar to <a href="http://www.developer.com/java/other/article.php/3300881/The-Essence-of-OOP-using-Java-Anonymous-Classes.htm" rel="nofollow noreferrer">anonymous classes Java</a> (concept code):</p>
  
  <pre><code>public static Tree&lt;T&gt; Insert&lt;T&gt;(T v, Tree&lt;T&gt; tree) where T : IComparable&lt;T&gt;
  {
      return tree.Accept&lt;Tree&lt;T&gt;&gt;(new InsertVisitor&lt;T&gt;()
          {
              public Tree&lt;T&gt; Visit(Nil&lt;T&gt; s) { return new Node&lt;T&gt;() { Left = Tree&lt;T&gt;.empty, Value = v, Right = Tree&lt;T&gt;.empty }; }
              public Tree&lt;T&gt; Visit(Node&lt;T&gt; s)
              {
                  switch (v.CompareTo(s.Value))
                  {
                      case -1: return new Node&lt;T&gt;() { Left = Insert(v, s.Left), Value = s.Value, Right = s.Right };
                      case 1: return new Node&lt;T&gt;() { Left = s.Left, Value = s.Value, Right = Insert(v, s.Right) };
                      default: return s;
                   }
              }
          };
  }
  </code></pre>
  
  <p><strong>Is there any way to simulate anonymous classes with interface implementations in C#?</strong></p>