C#需要运算符重载
我知道接口不能包含运算符,但有没有办法要求/告诉编译器+和/运算符将由某个类实现 我想这样做: public class AutoAverage<T> where T:(overloads + and /) { private List<T> history ; // 10-20 values public T NextAvg( T newValue ) { // implementation wants to push newValue // into history, then sum // values in history and return an average } }C#需要运算符重载,c#,C#,我知道接口不能包含运算符,但有没有办法要求/告诉编译器+和/运算符将由某个类实现 我想这样做: public class AutoAverage<T> where T:(overloads + and /) { private List<T> history ; // 10-20 values public T NextAvg( T newValue ) { // implementation wants to push newValue
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
}
}
公共类自动平均,其中T:(重载+和/)
{
私有列表历史记录;//10-20个值
公共T NextAvg(T newValue)
{
//实现希望推送新的价值
//进入历史,然后总结
//值并返回平均值
}
}
不幸的是,这在C#中目前是不可能的。不幸的是,这在C#中目前是不可能的。您可能能够使用Marc Gravel在这里的回答中描述的接口实现某个close:您可能能够实现某个close,使用Marc Gravell在这里的回答中描述的界面:我会尝试以下方法: public class AutoAverage<T> where T:(overloads + and /) { private List<T> history ; // 10-20 values public T NextAvg( T newValue ) { // implementation wants to push newValue // into history, then sum // values in history and return an average } }
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
}
}
接口ISumT
{
///
///sum方法将rhs添加到此值并返回结果
///
///
///使用接口(即ISumT)
///使用方法(即总和)
///因为“where”不支持操作符
///
T和(T rhs);
}
公共类自动平均,其中T:ISumT
{
公共T NextAvg(T newValue)
{
//可以假设T实现sum方法
}
}
我想试试这样的方法:
public class AutoAverage<T> where T:(overloads + and /)
{
private List<T> history ; // 10-20 values
public T NextAvg( T newValue )
{
// implementation wants to push newValue
// into history, then sum
// values in history and return an average
}
}
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
}
}
接口ISumT
{
///
///sum方法将rhs添加到此值并返回结果
///
///
///使用接口(即ISumT)
///使用方法(即总和)
///因为“where”不支持操作符
///
T和(T rhs);
}
公共类自动平均,其中T:ISumT
{
公共T NextAvg(T newValue)
{
//可以假设T实现sum方法
}
}
在这种情况下,有时我会将lambda传递给构造函数:
public class AutoAverage<T>
{
public AutoAverage(Func<T, T, T> sum, Func<T, T, T> divide) { ...
当你在做这样的二进制操作时,它有点难看(我经常在单参数函数中使用它,例如单位转换),但它有简单的优点
在您的情况下,这是否是一个好的解决方案将取决于您构建自动平均的频率。如果只创建一个或两个位置(对于每个T),那么传递lambda也不会那么糟糕。如果你把它构建成一大堆的地方,lambda会被复制得太多而不舒服。在这种情况下,有时我会将lambda传递给构造函数:
public class AutoAverage<T>
{
public AutoAverage(Func<T, T, T> sum, Func<T, T, T> divide) { ...
当你在做这样的二进制操作时,它有点难看(我经常在单参数函数中使用它,例如单位转换),但它有简单的优点
在您的情况下,这是否是一个好的解决方案将取决于您构建自动平均的频率。如果只创建一个或两个位置(对于每个T),那么传递lambda也不会那么糟糕。如果你把它构建成一大堆的地方,lambda会被复制得太多而不舒服。内置了通用的聚合函数(Sum
,Average
,等等),使用.NET 3.5在运行时提供
此外,C#4.0通过动态
(支持运算符)实现了这一点,但上述方法通常更快(间接性更小)。内置了通用聚合函数(求和
,平均
,等等),使用.NET 3.5在运行时提供
此外,C#4.0通过
动态
(支持运算符)实现了这一点,但上述方法通常更快(间接性更小)。这是一个经常要求的特性。要使其发挥作用,需要在CLR中进行大量工作,但这肯定是他们的目标。这是一个假设的未来版本可能的功能,但没有承诺。这是一个经常要求的功能。要使其发挥作用,需要在CLR中进行大量工作,但这肯定是他们的目标。这是假设的未来版本的一个可能功能,但没有承诺。。。。事实证明,为所有原语创建实现ISumT的类实际上有点乏味!。。。事实证明,为所有原语创建实现ISumT的类实际上有点乏味!