Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you could do is create a generic interface that includes the operations that you want to support, create a generic factory to create instances for the supported types to perform the operations, and use it.</p> <p>e.g.,</p> <pre><code>public interface IOperations&lt;T&gt; { T Add(T a, T b); T Subtract(T a, T b); T Multiply(T a, T b); T Divide(T a, T b); } public static class Operations&lt;T&gt; { public static IOperations&lt;T&gt; Default { get { return Create(); } } static IOperations&lt;T&gt; Create() { var type = typeof(T); switch (Type.GetTypeCode(type)) { case TypeCode.Byte: return (IOperations&lt;T&gt;)new ByteOperations(); case TypeCode.Single: return (IOperations&lt;T&gt;)new SingleOperations(); default: var message = String.Format("Operations for type {0} is not supported.", type.Name); throw new NotSupportedException(message); } } class ByteOperations : IOperations&lt;byte&gt; { public byte Add(byte a, byte b) { return unchecked ((byte)(a + b)); } public byte Subtract(byte a, byte b) { return unchecked ((byte)(a - b)); } public byte Multiply(byte a, byte b) { return unchecked ((byte)(a * b)); } public byte Divide(byte a, byte b) { return unchecked ((byte)(a / b)); } } class SingleOperations : IOperations&lt;float&gt; { public float Add(float a, float b) { return a + b; } public float Subtract(float a, float b) { return a - b; } public float Multiply(float a, float b) { return a * b; } public float Divide(float a, float b) { return a / b; } } } </code></pre> <pre><code>T Mean&lt;T&gt;(IList&lt;T&gt; numbers) { var operations = Operations&lt;T&gt;.Default; var sum = numbers.Aggregate(operations.Add); var count = (T)Convert.ChangeType(numbers.Count, typeof(T)); return operations.Divide(sum, count); } var resultByte = Mean(new byte[] { 1, 2, 3, 4 }); // 2 var resultSingle = Mean(new float[] { 1.1F, 2.1F, 3.1F, 4.1F }); // 2.6F var resultInt = Mean(new int[] { 1, 2, 3, 4 }); // not supported </code></pre> <p>If you don't mind a small performance hit, you could dynamically create the operations needed.</p> <pre><code>class GenericOperations&lt;T&gt; : IOperations&lt;T&gt; { public GenericOperations() { add = CreateLambda(Expression.Add); subtract = CreateLambda(Expression.Subtract); multiply = CreateLambda(Expression.Multiply); divide = CreateLambda(Expression.Divide); } private Func&lt;T, T, T&gt; add, subtract, multiply, divide; private static Func&lt;T, T, T&gt; CreateLambda(Func&lt;Expression, Expression, BinaryExpression&gt; op) { var a = Expression.Parameter(typeof(T), "a"); var b = Expression.Parameter(typeof(T), "b"); var body = op(a, b); var expr = Expression.Lambda&lt;Func&lt;T, T, T&gt;&gt;(body, a, b); return expr.Compile(); } public T Add(T a, T b) { return add(a, b); } public T Subtract(T a, T b) { return subtract(a, b); } public T Multiply(T a, T b) { return multiply(a, b); } public T Divide(T a, T b) { return divide(a, b); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload