相关疑难解决方法(0)

约束类型允许C#中的加/减操作(+/-)

这可能吗?

public interface Foo<TBar>
  where TBar : (can use the '+' and '-' operators)
Run Code Online (Sandbox Code Playgroud)

谢谢.

c# types

17
推荐指数
2
解决办法
2545
查看次数

如何使用C#泛型将这3种方法合并为一种?

我没有使用泛型,因此无法弄清楚是否可以使用泛型将以下三种方法合并为一种以减少重复.实际上我的代码目前有六种方法,但如果你可以解决这三种方法,那么其余的应该只使用相同的解决方案.

    private object EvaluateUInt64(UInt64 x, UInt64 y)
    {
        switch (Operation)
        {
            case BinaryOp.Add:
                return x + y;
            case BinaryOp.Subtract:
                return x - y;
            case BinaryOp.Multiply:
                return x * y;
            case BinaryOp.Divide:
                return x / y;
            case BinaryOp.Remainder:
                return x % y;
            default:
                throw new ApplicationException("error");
        }
    }

    private object EvaluateFloat(float x, float y)
    {
        switch(Operation)
        {
            case BinaryOp.Add:
                return x + y;
            case BinaryOp.Subtract:
                return x - y;
            case BinaryOp.Multiply:
                return x * y;
            case BinaryOp.Divide:
                return x / y; …
Run Code Online (Sandbox Code Playgroud)

c# generics

6
推荐指数
1
解决办法
1144
查看次数

标签 统计

c# ×2

generics ×1

types ×1