title: How can I make this simple method into a generic method?
body: <p>I have a method as follows:</p>
  
  <pre><code>int coerce(int val, int min=0, int max = 10)
  {
      if (val &lt; min)
          return min;
      if (val &gt; max)
          return max;
      return val;
  }
  </code></pre>
  
  <p>Now, I have to make it for <code>byte</code>, <code>float</code>, <code>double</code>, and other numeric types.</p>
  
  <p>As all we know, making numerous similar methods for those types is very ineffective, so I want to make it into a generic method.  The following is the what I tried to do:</p>
  
  <pre><code>T coerce&lt;T&gt;(T val, T min=(T)0, T max=(T)10) where T:IComparable
  {
      // ... same as the above ...
  }
  </code></pre>
  
  <p>I know that the code does not run, and that's why I'm asking for this.  I'm currently confused by two questions:</p>
  
  <ol>
  <li><p>How can I compare <code>T</code> types?</p>
  
  <p>Visual Studio warns about the operator <code>&lt;</code> and <code>&gt;</code>.  I tried to use <code>where T:IComparable</code> but it did not solve my problem.</p></li>
  <li><p>How can I set default values for a generic argument?</p>
  
  <p>I tried to use <code>(T)0</code> and <code>(T)10</code> for it.  But it was not a good choice, anyway.</p></li>
  </ol>
  
  <p>It may be a simple question, but I couldn't find answer from Google.</p>