I am trying to understand some VB code that does this:
Public Function Foo( ByVal a as Byte(), ByVal b as Byte(), ByVal Optional c as Byte() = Nothing) as Byte()
<snip>
c = If(c, New Byte(-1) {} )
<snip>
End Function
What does it mean to use -1
there? When I use a translator to convert the code to C#, it doesn't like -1 and throws an error that it's out of range and must be non-negative.
In VB.NET, you create an array by specifying the upper bound, e.g.
Dim arr = New Byte(99) {}
creates an array with a Length
of 100. If the upper bound is 1 less than the Length
and you specify -1 as the upper bound, what's the Length
? It's zero, i.e. you created an empty array. Since .NET Framework 4.6, you should be using the Array.Empty
method to do that:
c = If(c, Array.Empty(Of Byte)())
c = c ?? Array.Empty<byte>();
I think that C# would also support this:
c ??= Array.Empty<byte>();