
C# requiring operator overloading
By: a guest on Jan 28th, 2012 | syntax:
None | size: 0.71 KB | hits: 18 | expires: Never
public class AutoAverage<T>
{
public AutoAverage(Func<T, T, T> sum, Func<T, T, T> divide) { ...
public class AutoAverage<T>
{
public AutoAverage(Func<T, T, T> sum, Func<T, T, T> divide) { ...
var autoAverage = new AutoAverage((x, y) => x + y, (x, y) => x / y);
// ... use it ...
interface ISumT<T>
{
/// <summary>
/// the sum method adds rhs to this and returns the result
/// </summary>
/// <remark>
/// use a interface (i.e. ISumT)
/// with a method (i.e. sum)
/// because "where" doesn't support operators
/// </remark>
T sum(T rhs);
}
public class AutoAverage<T> where T : ISumT<T>
{
public T NextAvg( T newValue )
{
// can assume that T implements the sum method
}
}