什么是C#"检查"关键字呢? [英] What is the C# "checked" keyword for?

查看:141
本文介绍了什么是C#"检查"关键字呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是碰到这个关键字来的第一次。它是干什么的,什么时候应该应用它呢?



  INT乘以(INT I,诠释J)
{
返回检查(我* j)条;
}


解决方案

修改



埃里克利珀有两部分博客文章什么是对选中的关键字好不好?:的第1部分 - 的第2部分



< HR />

选中是块关键字,使算术溢出检查。通常,如果一个整数操作超出该类型可以处理,操作前进反正最大值或最小值,其结果就像一个里程表周期。因此,例如:

 字节B = byte.MaxValue; 
Console.WriteLine(二); // 255(11111111)
Console.WriteLine(++ b)条; // 0(00000000)

在此放置一个片段检查块防止溢出,而是运行时抛出的 发生OverflowException

 检查
{
字节b = byte.MaxValue;
Console.WriteLine(二); // B = 255

{
Console.WriteLine(++ b)条;
}
赶上(发生OverflowException E)
{
Console.WriteLine(e.Message); //算术运算导致溢出。
// B = 255
}
}

和自有一个编译器选项 /检查 ,果然编译器的默认检查的,也有在 选中 关键字,防止溢出检查。






至于用法,溢出检查应谨慎使用,一样,是一般异常处理如此。要检查是否在运行时溢出,这是显著较快(如,一个数量级)做一个简单的检查,而不是打开溢出检查:

  INT乘以(INT I,诠释J)
{
如果((长)为int *(长)J> int.MaxValue)
抛出新的InvalidOperationException异常( 溢出);
返回我*焦耳;
}

您甚至可以为Int64的做到这一点/长,使用的BigInteger(这可能是仍然至少一个数量级的速度比使用检查):

 长乘法(长我,龙J)
{
如果(新System.Numerics.BigInteger(我)+ J> long.MaxValue)
抛出新的InvalidOperationException异常( 溢出);
返回我*焦耳;
}






还有一个好代码项目,对本文解释了一些注意事项(如溢出只检查适用于眼前的代码块,而不是块内的任何函数调用)。


I just came across this keyword for the first time. What does it do, and when should it be used?

int multiply(int i, int j)
{
    return checked(i * j);
}

解决方案

Edit

Eric Lippert has a two-part blog post "What is the unchecked keyword good for?": Part 1 -- Part 2


"Checked" is a block keyword that enables arithmetic overflow checking. Normally, if an integer operation exceeds the maximum or minimum value that the type can handle, the operation proceeds anyway, and the result just cycles like an odometer. So, for example:

byte b = byte.MaxValue;
Console.WriteLine(b);       // 255  (11111111)
Console.WriteLine(++b);     // 0    (00000000)

Placing this snippet in a checked block prevents the overflow, and instead the runtime throws an OverflowException:

checked
{
    byte b = byte.MaxValue;
    Console.WriteLine(b);               // b=255
    try
    {
        Console.WriteLine(++b);
    }
    catch (OverflowException e)
    {
        Console.WriteLine(e.Message);   // "Arithmetic operation resulted in an overflow." 
                                        // b = 255
    }
}

And since there's a compiler option /checked, which turns compiler checking on by default, there is also the unchecked keyword which prevents overflow checking.


As far as usage, overflow checking should be used sparingly, as is true of exception handling in general. To check for an overflow at runtime, it's significantly faster (like, an order of magnitude) to do a simple check, rather than to turn on overflow checking:

int multiply(int i, int j)
{ 
    if ((long)int * (long)j > int.MaxValue)
        throw new InvalidOperationException("overflow");
    return i*j;
}

You can do this even for Int64/long, using BigInteger (this can be still at least an order of magnitude faster than using checked):

long multiply(long i, long j)
{ 
    if (new System.Numerics.BigInteger(i) + j > long.MaxValue)
        throw new InvalidOperationException("overflow");
    return i*j;
}


There's also a good Code Project article on this that explains some caveats (eg, the overflow check only applies to the immediate code block, not to any function calls inside the block).

这篇关于什么是C#&QUOT;检查&QUOT;关键字呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆