title: user-defined conversion to interface
body: <p>I just ran into the "user-defined conversions to or from an interface are not allowed" problem in C#.  What I was attempting to do was create a generic Graph class that could be iterated over in a couple different ways, depending on the supported interface.  So:</p>
  
  <pre><code>public class Graph&lt;T&gt; : IBreadthFirstSearchTree&lt;T&gt;, IDepthFirstSearchTree&lt;T&gt;
  {
      // unnecessary details
  
      public static explicit operator IBreadthFirstSearchTree&lt;T&gt;(Graph&lt;T&gt; g)
      {
          g.enumerator = new GraphEnumerator&lt;T&gt;(g, SortStrategy.BreadthFirst);
          return g as IBreadthFirstSearchTree&lt;T&gt;;
      }
  
      public static explicit operator IDepthFirstSearchTree&lt;T&gt;(Graph&lt;T&gt; g)
      {
          g.enumerator = new GraphEnumerator&lt;T&gt;(g, SortStrategy.DepthFirst);
          return g as IDepthFirstSearchTree&lt;T&gt;;
      }
  }
  </code></pre>
  
  <p>was intended for this use:</p>
  
  <pre><code>foreach (GraphNode&lt;T&gt; gn in myGraph as IDepthFirstSearchTree)
  {
      // do stuff with gn
  }
  </code></pre>
  
  <p>Anyone know how I can achieve the same syntactic results within the constraints of the language?</p>