是否有一些"where"类型的约束可以添加以使下面的代码编译?
public class Plus<T> : BinaryOperator<T> where T : ...
{
public override T Evaluate(IContext<T> context)
{
return left.Evaluate(context) + right.Evaluate(context);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
可能重复:
C#中泛型类的算术运算符重载
这是我创建的泛型类的代码,用于将复数添加到重载运算符.
public class Complex<T>
{
public T _a, _b;
public Complex(T i, T j)
{
_a = i;
_b = j;
}
public static Complex<T> operator +(Complex<T> i, Complex<T> j)
{
return new Complex<T>(i._a + j._a, i._b + j._b);
}
}
Run Code Online (Sandbox Code Playgroud)
在使用这个时,我有一个错误,
Error: Operator '+' cannot be applied to operands of type 'T' and 'T'
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议我使用泛型运算符重载的方式吗?
可能重复:
.NET泛型中重载运算符约束的解决方案
我有一个问题我正在努力,目前它正在为ints 工作,但我希望它适用于所有可以使用+运算符添加的类.有没有办法在通用中定义它?例如,
public List<T> Foo<T>() where T : ISummable
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
编辑:
传递代理进行求和而不是使用+ =类型的Int的性能最好慢540%.调查可能的其他解决方案
最终解决方案:
谢谢大家的建议.我最终找到了一个不太慢的解决方案,并在编译时强制执行检查.当一位同事帮我解决这个问题时,我无法完全信任.无论如何,这里是:
以函数的形式实现一个包含所有必需操作符的接口
public interface IFoo<InputType, OutputType>
{
//Adds A to B and returns a value of type OutputType
OutputType Add(InputType a, InputType b);
//Subtracts A from B and returns a value of type OutputType
OutputType Subtract(InputType a, InputType b);
}
Run Code Online (Sandbox Code Playgroud)
创建要定义的类,但不使用Where子句,而是使用IFoo接口的依赖注入实例.OutputType通常是双倍的,因为操作的性质是数学的.
public class Bar<T>
{
private readonly IFoo<T,double> _operators;
public Bar(IFoo<T, double> operators)
{
_operators = operators;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当您使用此类时,您可以像这样定义操作规则: …
如果给出两种类型(类型a,类型b),是否有任何"好"的方法来确定这两个是否可以进行比较,求和等?
我在想如果类型实现IConvertible,可以将两者都转换成让我们说十进制并执行" Convert.ToDecimal(a) > Convert.ToDecimal(b)"?
我正在构建一个表达式求值程序,并希望能够处理任何类型的对象,因此需要知道一个类型是否可以与另一个类型进行比较(它不必在两侧都是相同的类型.例如.double > int)
假设我们有一个泛型类的想法Matrix<T>哪里T是数字型(Complex或double或者float,int等).
很自然,我们在C#中从in float到doublefrom double进行了隐式转换Complex.一般规则是我们有从较小类型到较大类型的隐式转换.到现在为止还挺好.
现在假设我们正在实现我们的Matrix<T>类型.由于这种新型的方式是数字也(或者至少其持有的数值),这是很自然有隐式转换从Matrix<float>到Matrix<double>,从Matrix<double>对Matrix<Complex>等至少它是好的,有这些像乘数学运算,加等但是这似乎无法正确实现,因为隐式运算符要求至少一个类型与我们实现它的类相同.
示例:下面的代码无法编译,甚至认为它可以解决我的问题.
public abstract partial class Matrix<T>
{
/// <summary>
/// Implicitly converts a matrix to double precision complex numbers.
/// </summary>
public static implicit operator Matrix<Complex64>(Matrix<double> matrix)
{
matrix.ToComplex();
}
/// <summary>
/// Implicitly converts a matrix to double precision real numbers.
/// </summary>
public static implicit …Run Code Online (Sandbox Code Playgroud) 我写了Generics类,但我遇到了标题中描述的问题.
class Program
{
static void Main(string[] args)
{
int a = 1;
int b = 2;
int c = 3;
dynamic obj = new Gen<int>();
obj.TestLine1(ref a, ref b);
obj = new Gen<string>();
obj.TestLine2(ref a, ref b, ref c);
System.Console.WriteLine(a + " " + b);
Console.ReadLine();
}
}
public class Gen<T>
{
public void TestLine1(ref T a, ref T b)
{
T temp;
temp = a;
a = b;
b = temp;
}
public void TestLine2(ref …Run Code Online (Sandbox Code Playgroud)