C#lambda /匿名代表中的词法范围

And*_*Dog 4 c# lambda delegates lexical-scope

我想检查一个简单的数学表达式是否会溢出(使用checkedcatch(OverflowException)),但不需要每次都使用try-catch块.因此表达式(不是结果!)应该传递给一个函数checkOverflow,然后在溢出的情况下相应地起作用.

这是我尝试过的,但不起作用,因为似乎没有lambda表达式的词法作用域.

static void Main(string[] args)
{
    double g, h;

    // Check if the expression g+h would overflow, *without* the need to use
    // try/catch around the expression

    // Both of the following attempts fail because there's no lexical scoping
    checkOverflow((FloatReturningExpression) delegate() {return g+h;});
    checkOverflow(() => g+h);

    Console.ReadKey();
}

private static void checkOverflow(FloatReturningExpression exp)
{
    try
    {
        checked { double f = exp(); }
    }
    catch(OverflowException)
    {
        Console.WriteLine("overflow!");
    }
}

private delegate double FloatReturningExpression();
Run Code Online (Sandbox Code Playgroud)

那有什么解决方案吗?(使用.NET 2,但不一定.)

Shu*_*oUk 7

.Net中的浮点数不会以整数运算的方式溢出.

他们有帮助地转到Double.PositiveIfinity,Double.NegativeIfinity或(特定于数学运算变为无效的情况Double.Nan)

请注意,由于浮点行为在面对具有非常不同精度的两个数字时,这也稍微复杂一些.

Console.WriteLine(double.MaxValue);
Console.WriteLine(double.MaxValue * 2);
Console.WriteLine(double.MaxValue + 1);
Console.WriteLine(double.MaxValue + double.MaxValue);
Run Code Online (Sandbox Code Playgroud)

得到:

1.79769313486232E+308
Infinity
1.79769313486232E+308
Infinity
Run Code Online (Sandbox Code Playgroud)

还不清楚你想要checkOverflow函数做什么,只是说它发生了吗?

如果这是所有这种方法都可行(我为你转换为int)

void Main()
{
    int a, b;
    a = int.MaxValue;
    b = 1;

    // Check if the expression a+b would overflow, *without* the need to use
    // try/catch around the expression
    checkOverflow(() => {checked { return a+b; }});    
}       

private static void checkOverflow(Func<int> exp)
{
    try
    {
        exp();
    }
    catch(OverflowException)
    {
        Console.WriteLine("overflow!");
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该补充其工作原因:
在影响变量的意义上,check不是词法范围.这是一个由编译器解释的区域,这里所有执行整数运算的代码都应该生成溢出陷阱指令.变量来自何处,只在哪里定义代码.

我相信你的心理模型是这样的:

checked  // enter a 'checked' state where all operations 
{        // (say on the current thread) are checked

code, function calls, etc. etc

}  // leave the checked mode, all operations are now unchecked
Run Code Online (Sandbox Code Playgroud)

这不是检查工作的方式,检查定义了在编译时发出的指令(某些指令陷阱溢出,有些则没有)

选中的块不会影响在其外部定义的代码.例如,只使用功能:

int Times2(int a)
{
    return a * 2;
}

void TheresNoDifferenceHere()
{
    checked { Times2(int.MaxValue); }
    Times2(int.MaxValue);
}
Run Code Online (Sandbox Code Playgroud)

Times2函数调用解析为类似的东西

IL_0000:  nop         
IL_0001:  ldarg.1     
IL_0002:  ldc.i4.2    
IL_0003:  mul         
IL_0004:  stloc.0     
IL_0005:  br.s        IL_0007
IL_0007:  ldloc.0     
IL_0008:  ret   
Run Code Online (Sandbox Code Playgroud)

如果你曾经使用过

int Times2(int a)
{
    checked { return a * 2; }
}

IL_0000:  nop         
IL_0001:  nop         
IL_0002:  ldarg.1     
IL_0003:  ldc.i4.2    
IL_0004:  mul.ovf     
IL_0005:  stloc.0     
IL_0006:  br.s        IL_0008
IL_0008:  ldloc.0     
IL_0009:  ret         
Run Code Online (Sandbox Code Playgroud)

注意使用mul和mul.ovf的区别.因此,对它的两次调用在事后不能改变它以进行检查.上面示例中第一次调用周围的已检查块实际上对生成的IL 没有影响.其中没有定义的操作对它很重要.

因此,您最初的想法是在一个地方定义算术运算(不检查),然后在另一个点运行它(就像函数调用一样),但'checked'指令不会影响它在编译时没有包围的代码.

lambdas解析为表达式树或匿名委托(可能由所需的合成类支持,以保存和维护任何与闭包相关的变量).在这两种情况下,它们的任何部分的检查方面都是在定义它们的地方完全定义的,而不是在它们被调用的地方.