This is simply not possible directly because you cannot constrain a generic type to be numeric (there is no "INumeric" or "IIntegral" interface, for instance, which the integer, float, etc. types might implement). However, several workarounds exist :
.NET 2.0 1. <http://www.codeproject.com/KB/cs/genericnumerics.aspx> - This article uses code from Eric Gunnerson's blog and demonstrates using an abstract class that needs to be specialized for each type specification. .NET 3.x: 1. <http://rogeralsing.com/2008/02/27/linq-expressions-calculating- with-generics> - This article shows how to use Linq Expression trees to accomplish this. 2. <http://www.yoda.arachsys.com/csharp/genericoperators.html> - This article on Jon Skeet's site uses the MiscUtil library developed by Marc Gravell to accomplish this. Its USP is that efficiency has not been sacrificed. Internally, the library also uses Expression tree lambdas. .NET 4.0: 1. <http://geekswithblogs.net/sdorman/archive/2008/11/16/c-4.0-dynamic- programming.aspx> - C# 4.0 introduces the "dynamic" keyword which allows method calls to be resolved at runtime, rather than blocked by the compiler. This provides another (cleaner) solution to the problem.
