Guest

C# requiring operator overloading

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 0.71 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. public class AutoAverage<T>
  2. {
  3.     public AutoAverage(Func<T, T, T> sum, Func<T, T, T> divide) { ...
  4.        
  5. public class AutoAverage<T>
  6. {
  7.     public AutoAverage(Func<T, T, T> sum, Func<T, T, T> divide) { ...
  8.        
  9. var autoAverage = new AutoAverage((x, y) => x + y, (x, y) => x / y);
  10. // ... use it ...
  11.        
  12. interface ISumT<T>
  13. {
  14.   /// <summary>
  15.   /// the sum method adds rhs to this and returns the result
  16.   /// </summary>
  17.   /// <remark>
  18.   /// use a interface (i.e. ISumT)
  19.   /// with a method (i.e. sum)
  20.   /// because "where" doesn't support operators
  21.   /// </remark>
  22.   T sum(T rhs);
  23. }
  24.  
  25. public class AutoAverage<T> where T : ISumT<T>
  26. {
  27.   public T NextAvg( T newValue )
  28.   {
  29.     // can assume that T implements the sum method
  30.   }
  31. }