title: c# enforcing specific method signature in derived classes (abstract virtual static)
body: <p>I have an <code>abstract class BaseClass</code> and want to enforce that all <code>class Derived: BaseClass</code> have to override a method <code>foo</code> resulting in a very specific signature. The Problem is as following:</p>
  <ol>
  <li>Classes which inherit from BaseClass <strong>have to implement</strong> <code>foo</code> on their own (pref. only if they are not abstract) [like <code>abstract</code>]</li>
  <li>BaseClass must have a <strong>base-implementation</strong>, derived classes can call [like <code>virtual</code>]</li>
  <li>[<strong>Signature</strong> of <code>foo</code> in derived classes should be <code>static</code> (the classes themself are not!) [eddit: omitted]]</li>
  <li><strong>Arguments</strong> of <code>foo</code> are of <code>class Derived</code>/<code>this.GetType()</code> (pref. without changing signature of BaseClass)</li>
  </ol>
  <p>It is already hard enough/ impossible(?) to combine two of these (exept 4.), but maybe there are some clever workarounds and I take what I can get. At last here is some pseudo code to better reflect my intention:</p>
  <pre><code>public abstract class BaseClass
  {
      bool baseBool;
      public abstract virtual static bool foo(BaseClass b1, BaseClass b2)
      {
          //sample code
          return b1.baseBool ^ b2.baseBool;
      }
  }
  
  public class Derived: BaseClass
  {
      bool derivedBool;
      public static override bool foo(Derived d1, Derived d2)
      {
          //sample code
          return base.foo(d1, d2) &amp;&amp; d1.derivedBool ^ d2.derivedBool;
      }
  }
  </code></pre>
  <h2>Solution based on accepted answer by lidqy</h2>
  <pre class="lang-cs prettyprint-override"><code>public abstract class BaseClass
  {
      bool baseBool;
      public abstract bool foo(BaseClass b1, BaseClass b2);
  }
  
  public abstract class BaseClassWrappedCRTP&lt;T&gt;: BaseClass where T: BaseClassWrappedCRTP&lt;T&gt;
  {
      public override bool foo(BaseClass b1, BaseClass b2)
      {
          //sample code
          return b1.baseBool ^ b2.baseBool &amp;&amp; fooWrappedCRTP((T)b1, (T)b2);
      }
  
      protected abstract bool fooWrappedCRTP(T d1, T d2);
  }
  
  public class Derived: BaseClassWrappedCRTP&lt;Derived&gt;
  {
      bool derivedBool;
      protected override bool fooWrappedCRTP(Derived d1, Derived d2)
      {
          //sample code
          return d1.derivedBool ^ d2.derivedBool;
      }
  }
  </code></pre>
  <p>Note that if Derived were <code>abstract class Derived: BaseClassWrappedCRTP&lt;Derived&gt;</code>, we would need <code>abstract class DerivedWrappedCRTP&lt;T&gt;: Derived where T: DerivedWrappedCRTP&lt;T&gt;</code> to continue inheritance of <code>foo</code> in this way (Given an actual need to access non-generic <code>Derived</code>).</p>