4

I'm converting to VB.NET, which doesn't provide the unchecked keyword. But it appears to be unnecessary in this statement:

const int dwAccess = unchecked((int)0xC0000000);

I have two observations here:

  1. dwAccess is declared as a constant
  2. The value assigned falls well within the range of System.Int32

Given these, will it be safe to just go with this:

Const dwAccess As Integer = &HC0000000

It's being used in this context:

[DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
private static extern SafeFileHandle CreateFile(string lpFileName, int dwAccess, int dwShareMode,
                                                IntPtr securityAttrs, int dwCreationDisposition,
                                                int dwFlagsAndAttributes, IntPtr hTemplateFile);

Clarification: This question is not about whether the unchecked keyword is necessary in C#. Clearly it is. It's about whether the absence of the keyword in VB.NET precludes successful conversion of the statement.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
InteXX
  • 6,135
  • 6
  • 43
  • 80
  • isn't 0xC0000000 **out of the range** for a System.Int32? https://dotnetfiddle.net/ulwdVX – Gian Paolo May 12 '18 at 07:45
  • @HenkHolterman yes, but Int32.MaxValue is =0x7FFFFFFF; for higher numbers, you have to use UInt32, ain't it? – Gian Paolo May 12 '18 at 07:53
  • @GianPaolo: I've found that the value is the same in both VB [(link)](https://dotnetfiddle.net/HwIa79) and C# [(link)](https://dotnetfiddle.net/tNfQv8). – InteXX May 12 '18 at 13:12
  • I think I understand. It seems VB is `unchecked` by default, while c# require specifying it if you want to assign 0xC0000000 to a System.Int32 variable – Gian Paolo May 12 '18 at 14:00
  • @GianPaolo — Yes, it seems that way. – InteXX May 12 '18 at 15:00
  • 1
    No, other way around. VB.NET is always checked with default project settings. And C# is not. That made life difficult for the C# team, they had to make an effort to do the checking at compile-time. Not done so well, hard to do. The VB.NET compiler is quite happy with overflow at compile-time, like it always was. – Hans Passant Sep 29 '18 at 22:35

2 Answers2

3

The answer is a yes. It's redundant and can be removed.

unchecked (C# Reference)

The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions.


Update, this answer should have been qualified.

If the original VB.NET code was unchecked then the equivalent C# code would function as is unchecked as well in the same way. Both will overflow and both will give the same results.

InteXX
  • 6,135
  • 6
  • 43
  • 80
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • That's what I thought; thank you very much. (p.s. I can accept your answer in 11 minutes.) – InteXX May 12 '18 at 05:38
  • @SeanVikoren: The keyword is required in C#, and VB.NET would reject an attempt to assign 3221225472 to an `Int32` or `Integer`. In vb.net, however, unsuffixed hex constants in the range &h80000000 to &hFFFFFFFF are interpreted as negative values of type Int32. – supercat Sep 28 '18 at 22:39
  • @supercat The question needs editing. I will undo the down-vote as soon as I can. – Sean Vikoren Sep 28 '18 at 23:15
  • Of course, you could use a uint and avoid the negative – Basic Sep 29 '18 at 19:55
  • @Basic ~ Actually, that's a very good point. According to [this](https://www.pinvoke.net/default.aspx/kernel32.CreateFile), `uint` will work just fine. – InteXX Sep 30 '18 at 02:26
0

Yes. The following is fine:

Const dwAccess As Integer = &HC0000000

There is no unchecked keyword in VB.Net, but there is a compile flag -removeintchecks 1 that will cause integer overflow to not be checked for the entire program.

If the C# line is:

const int dwAccess = unchecked((int)0xC0000000);

then the VB.Net translation will look like this:

Const dwAccess As Integer = &HC0000000

Since 0xC0000000 is within the range of uint, but not int, the unchecked keyword in C# is used to convert the value to the int format. In VB.Net, the conversion from a hex constant is automatic.

Sean Vikoren
  • 1,084
  • 1
  • 13
  • 24
  • 1
    question is about c# ---> vb.net and was correctly answered a few months ago – Fredou Sep 28 '18 at 22:31
  • 1
    The question is confusing. The question is not whether the `unchecked` is necessary in C#. It is necessary, as you note. The question is whether some equivalent is necessary when porting the code to VB. It is not; VB allows unchecked arithmetic on integer constants by default. – Eric Lippert Sep 28 '18 at 22:43
  • 1
    @EricLippert: VB.NET does not allow unchecked arithmetic on integer constants, but regards an unsuffixed &C0000000 as a negative integer constant. This behavior is analogous to how VB6 handled 16-bit hex constants, but I don't think it should have carried through to VB.NET, though there should have been an easy function to convert unsigned->signed quantities with wrapping. – supercat Sep 29 '18 at 03:11
  • I think you might want to uncheck my answer as correct – TheGeneral Sep 29 '18 at 10:17
  • @Saruman ~ Thank you for your refreshing candor. Yes, with your consent here I'll ask Eric to post his clarification as an answer so I can accept it. – InteXX Sep 29 '18 at 18:32
  • @EricLippert ~ Yes, you are correct. Thank you. Would you consider posting your clarification as an answer so I switch to yours. Also: I've edited my question, both title and body. – InteXX Sep 29 '18 at 18:34
  • @TheGeneral ~ I just happened to see your comment just now (it wasn't addressed directly to me, so I wasn't notified at the time). Are you still of the same mind, that I might accept a different answer? – InteXX May 12 '19 at 05:04