c# templating integer methods?
<p>Is it possible to template methods for any kind of integer size ?</p>

<p>To illustrate, imagine this very trivial example (the body of the method is not important in my question):</p>

<pre><code>public int Mul(int a, int b)    {
    return a*b;
}
</code></pre>

<p>Now, I want the same method that supports any kind of integer (excluding BigInteger of course). I have to write all variants :</p>

<pre><code>public long Mul(long a, long b)    {
    return a*b;
}
public ulong Mul(ulong a, ulong b)    {
    return a*b;
}
public short Mul(short a, short b)    {
    return a*b;
}
public ushort Mul(ushort a, ushort b)    {
    return a*b;
}
public byte Mul(byte a, byte b)    {
    return a*b;
}
</code></pre>

<p>While this example is very trivial and it's not actually a problem to duplicate, if I have more complex algorithms like this (replicate for all integer kinds):</p>

<pre><code>    public static IEnumerable&lt;long&gt; GetPrimesFactors(this long number)
    {
        for (long i = 2; i &lt;= number / 2; i++)
        {
            while (number % i == 0)
            {
                yield return i;
                number /= i;
            }
        }
        yield return number;
    }
</code></pre>

<p>it introduces a maintenance risk as there is duplicated code and logic (coding integrists would say this is the evil to have same logic at multiple place).</p>

<p>Some of you may suggest to implements the long version and cast the result, but having to ask consumer code to cast can be confusing and reduce readability :</p>

<pre><code>void SomeMethod(IEnumerable&lt;int&gt; valuesToProcess)
{
    foreach(int value in valuesToProcess) { Console.WriteLine(value); }
}

void Main()
{
    int i = 42;
    SomeMethod(((long)i).GetPrimesFactors().Select(l=&gt;(int)l)); 
    SomeMethod(GetPrimesFactors(i));

    long l = 42L;
    SomeMethod(l.GetPrimesFactors().Select(l=&gt;(int)l));
}
</code></pre>

<p>When I see the definition of the interface <code>IEnumerable&lt;T&gt;</code>, and especially the definitions of <code>Sum</code> method overloads :</p>

<pre><code>    public static decimal? Sum(this IEnumerable&lt;decimal?&gt; source);
    public static decimal Sum(this IEnumerable&lt;decimal&gt; source);
    public static double? Sum(this IEnumerable&lt;double?&gt; source);
    public static double Sum(this IEnumerable&lt;double&gt; source);
    public static float? Sum(this IEnumerable&lt;float?&gt; source);       
    public static float Sum(this IEnumerable&lt;float&gt; source);
    public static int? Sum(this IEnumerable&lt;int?&gt; source);
    public static int Sum(this IEnumerable&lt;int&gt; source);
    public static long? Sum(this IEnumerable&lt;long?&gt; source);
    public static long Sum(this IEnumerable&lt;long&gt; source);
</code></pre>

<p>I conclude that it's not possible... that's why MS has to implement all overloads.</p>

<p>Does anyone have any tips for designing general purpose integer methods without having to duplicate logic ?</p>
