如何在C#中使用多个未选中的项 [英] How to use multiple uncheckeds in c#

查看:110
本文介绍了如何在C#中使用多个未选中的项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以对整个程序或整个块使用未选中状态?

Is there a way to use unchecked for a whole program or a whole block ?

我正在翻译Java类型的东西,它的类型很长,并且有很多无符号长常量的比较...有些地方有一些开关,有20〜30种情况...我是否必须逐个取消选中每种情况还是有更快/更轻松的方法?

I'm translating something from java that has the type long and lots of comparisons with constants that are unsigned long... Some places there a some switch's with 20~30 cases... Do I have to uncheck every case individualy or is there a faster/easier way to do it?

case 101: 
   return jjMoveStringLiteralDfa5_0(active0, 0x8002010000000000L, active1, 0x1L);

我必须更改为:

   return jjMoveStringLiteralDfa5_0(active0, unchecked((long)0x8002010000000000L), active1, 0x1L);

但是有很多情况...而且它是在具有大量IF的解析器生成器中,因此最好在整个文件中隐藏一些检查,而不是搜索所有可能生成未签名的位置的东西长常量...

but there are many cases... and it is in a parser generator with lots of IF's, so It would be better to have something to suppress those checks in the whole file instead of searching every possible place that would generate those unsigned long constants...

有一种方法可以在Visual Studio选项上进行设置,但是由于我正在生成解析器,所以我想知道是否可以自动使该解析器不检查是否有上溢/下溢吗?

There is a way to set that on Visual Studio options but since I'm generating a parser I wanted to know if I could make that parser automaticaly not check for overflows/underflows, is that possible?

推荐答案

如果要使用十六进制文字通过二进制补码表示 个整数,则只需必须提供unchecked关键字的强制转换和显式使用.示例:

If you want to use hexadecimal literals to express negative integers via the two's complement convention, you simply have to provide both a cast and an explicit use of the unchecked keyword. Example:

sbyte b = unchecked((sbyte)0xAB);
short s = unchecked((short)0xABCD);
int i = unchecked((int)0xABCDABCD);
long l = unchecked((long)0xABCDABCDABCDABCD);

仅将默认上下文(由C#编译器开关和/或csproj文件定义)作为未经检查"上下文是不够的.

It is not enough that the default context (as defined by C# compiler switch and/or csproj file) is the "unchecked" context.

如果可以使用无符号类型(例如uint),则不会出现此问题.但是BCL通常不使用无符号类型,并且不将其视为符合CLS".

If you can use the unsigned types (for example uint), you won't have this problem. But the unsigned types are typically not used by the BCL, and are not considered "CLS compliant".

只需完全弄清楚,使用 文字,您就不需要强制转换也不需要unchecked关键字:

Just make it entirely clear, with positive literals you need neither cast nor unchecked keyword:

sbyte b = 0x2E;
short s = 0x2E3F;
int i = 0x2E3F2E3F;
long l = 0x2E3F2E3F2E3F2E3F;

有关常量表达式的隐式转换可以简化此操作,请参见隐式常量表达式转换.

For constant expressions implicit conversions exist to make this easy, see Implicit constant expression conversions.

这篇关于如何在C#中使用多个未选中的项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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