Fastest way to do an unchecked integer addition in VB.Net?

I know this is old, but I recently needed to convert some C# code that used unchecked and I thought I'd share how I did it. It's pure VB code and can be scoped however you need (rather than a project-wide option).

The trick is to create a structure that contains a Long field and two Integer fields. Then use StructLayout and FieldOffset attributes to create a union of the long and the two integers. The fields can (should) be private. Use widening CType operators to convert from a Long to the structure and from the structure to an Integer (using the low integer value). Add operator overloads for +, -, *, etc... and presto! unchecked arithmetic in VB!

Sort of... it will still overflow, as Strilanc pointed out, if the long value goes outside the range for longs. But it works pretty well for a lot of situations where unchecked is used.

Here's an example:

<StructLayout(LayoutKind.Explicit)>
Public Structure UncheckedInteger

    <FieldOffset(0)>
    Private longValue As Long
    <FieldOffset(0)>
    Private intValueLo As Integer
    <FieldOffset(4)>
    Private intValueHi As Integer

    Private Sub New(newLongValue As Long)
        longValue = newLongValue
    End Sub

    Public Overloads Shared Widening Operator CType(value As Long) As UncheckedInteger
        Return New UncheckedInteger(value)
    End Operator

    Public Overloads Shared Widening Operator CType(value As UncheckedInteger) As Long
        Return value.longValue
    End Operator

    Public Overloads Shared Widening Operator CType(value As UncheckedInteger) As Integer
        Return value.intValueLo
    End Operator

    Public Overloads Shared Operator *(x As UncheckedInteger, y As Integer) As UncheckedInteger
        Return New UncheckedInteger(x.longValue * y)
    End Operator

    Public Overloads Shared Operator Xor(x As UncheckedInteger, y As Integer) As UncheckedInteger
        Return New UncheckedInteger(x.longValue Xor y)
    End Operator

    ' Any other operator overload you need...
End Structure

Use the structure in code like this:

Dim x As UncheckedInteger = 2147483647
Dim result As Integer = x * 2  ' This would throw OverflowException using just Integers

Console.WriteLine(result.ToString())  ' -2

Careful that your calculations don't overflow before assigning the result to an UncheckedInteger. You could create UncheckedShort and UncheckedByte structures using the same technique.


Personally, I think leaving this in its own assembly, especially since it'll be such a small assembly, is a good option. This makes maintenance easier, since it's easy to regenerate this assembly at any time. Just make a separate assembly flagged unchecked, and put your performance-sensitive code there.