title: Can I make a generic optional, defaulting to a certain class?
body: <p>My question is related to <a href="https://stackoverflow.com/questions/707780/is-there-a-reasonable-approach-to-default-type-parameters-in-c-sharp-generics">Is there a reasonable approach to &quot;default&quot; type parameters in C# Generics?</a>, but using an inner generic class that approach doesn't work.</p>
  
  <p>Given code like this:</p>
  
  <pre><code>using System;
  
  public class FooEventArgs&lt;T&gt; : EventArgs
  {
      // ... T properties and a constructor
  }
  
  public class Foo&lt;T&gt;
  {
      public delegate void EventHandler&lt;FooEventArgs&gt;(object sender, FooEventArgs&lt;T&gt; e);
      public event EventHandler&lt;FooEventArgs&lt;T&gt;&gt; Changed
  }
  </code></pre>
  
  <p>And with it being used like this:</p>
  
  <pre><code>public class User
  {
      public Foo&lt;int&gt; foo1;
      public Foo&lt;object&gt; foo2;
  
      public User()
      {
          foo1 = new Foo&lt;int&gt;();
          foo2 = new Foo&lt;object&gt;();
          foo1.Changed += foo1_Changed;
          foo2.Changed += foo2_Changed;
      }
  
      protected void foo1_Changed(object sender, FooEventArgs&lt;int&gt; e) { ... }
      protected void foo2_Changed(object sender, FooEventArgs&lt;object&gt; e) { ... }
  }
  </code></pre>
  
  <p>Well, I'd rather like it if I could have the generic optional, as there will be many cases where I don't know what type something will be coming in. (Data is coming from an external system which has its own variable types, which are then converted into .NET types, but I run into situations where, for example, one remote data type may turn into one of a couple of .NET types, or where it is of the "any" type—thus <code>object</code> would be the only real answer for that case.)</p>
  
  <p>The solution which immediately occurred to me was subclassing (it was also the primary suggestion in the question linked to earlier):</p>
  
  <pre><code>public class Foo : Foo&lt;object&gt;
  {
      public Foo(...) : base(...) { }
  }
  
  public class FooEventArgs : FooEventArgs&lt;object&gt;
  {
      public Foo(...) : base(...) { }
  }
  </code></pre>
  
  <p>I then want to use it like this:</p>
  
  <pre><code>public class User
  {
      public Foo foo3;
  
      public User()
      {
          foo3 = new Foo();
          foo3.Changed += foo3_Changed;
      }
  
      protected void foo3_Changed(object sender, FooEventArgs e) { ... }
  }
  </code></pre>
  
  <p>The problem is that it naturally won't work with <code>foo3_Changed</code> accepting <code>FooEventArgs</code>; it needs <code>FooEventArgs&lt;object&gt;</code>, as that's what the <code>Foo.Changed</code> event will get pass to it (as the value will come from <code>Foo&lt;object&gt;</code>).</p>
  
  <pre><code>Foo.cs(3,1415926): error CS0123: No overload for 'foo3_Changed' matches delegate 'FooLibrary.Foo&lt;object&gt;.EventHandler&lt;FooLibrary.FooEventArgs&lt;object&gt;&gt;'
  </code></pre>
  
  <p>Is there anything I can do about this, short of duplicating much of the class?</p>
  
  <p>I did try one other thing: an implicit operator to convert from <code>FooEventArgs&lt;object&gt;</code> to <code>FooEventArgs</code>.</p>
  
  <pre><code>    public static implicit operator FooEventArgs(FooEventArgs&lt;object&gt; e)
      {
          return new FooEventArgs(...);
      }
  </code></pre>
  
  <p>This, unfortunately, doesn't seem to work, though I'm not quite clear on why:</p>
  
  <pre><code>EditBuffer.cs(13,37): error CS0553: 'FooLibrary.FooEventArgs.implicit operator FooLibrary.FooEventArgs(FooLibrary.FooEventArgs&lt;object&gt;)': user-defined conversions to or from a base class are not allowed
  </code></pre>
  
  <p>So then, once again, is there anything I can do about this, or am I correct in thinking that it's Tough Luck and I'll just have to be content using <code>FooEventArgs&lt;object&gt;</code> (and then I guess I may as well just use <code>Foo&lt;object&gt;</code>)?</p>