Generic Interface for binary operation : Generic Interface « Generic « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Text;

  public interface IBinaryOperations<T>
  {
    T Add(T arg1, T arg2);
    T Subtract(T arg1, T arg2);
    T Multiply(T arg1, T arg2);
    T Divide(T arg1, T arg2);
  }

  public class BasicMath : IBinaryOperations<int>
  {
    public BasicMath() {}

    public int Add(int arg1, int arg2)
    { return arg1 + arg2; }

    public int Subtract(int arg1, int arg2)
    { return arg1 - arg2; }

    public int Multiply(int arg1, int arg2)
    { return arg1 * arg2; }

    public int Divide(int arg1, int arg2)
    { return arg1 / arg2; }

    }

  class Program
  {
    static void Main(string[] args)
    {
      BasicMath m = new BasicMath();
      Console.WriteLine("1 + 1 = {0}", m.Add(1, 1));
    }
  }








18.17.Generic Interface
18.17.1.Generic Interface
18.17.2.Implement multiple generic interfaces by a non-generic class
18.17.3.Generic IEquatable
18.17.4.Declaring a Generic Interface, Implementing a Generic Interface
18.17.5.Declaring a Generic with Multiple Type Parameters
18.17.6.Generic Interface for binary operation