Is the unchecked keyword necessary in the VB.NET version of this C# statement?

InteXX

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.

The General

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related