Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET "For each" versus ".GetUpperBound(0)"

I would like to know what is preferred...

    Dim sLines() As String = s.Split(NewLine)

For each:

    For Each sLines_item As String In sLines

.GetUpperBound:

    For i As Integer = 0 To sLines.GetUpperBound(0)

I have no idea why the "For Each" was introduced for such cases. Until now I have only used .GetUpperBound, and I don't see any PRO for the "For Each".

Thank you

ps: When I use ."GetUpperBound(0)", I do know that I am iterating over the vector. The "For Each" in contrast sounds like "I don't care in which order the vector is given to me". But that is just personal gusto, I guess.

like image 916
tmighty Avatar asked Oct 20 '22 10:10

tmighty


1 Answers

Short answer: Do not use GetUpperBound(). The only advantage of GetUpperBound() is that it works for multi-dimensional arrays, where Length doesn't work. However, even that usage is outdated since there is Array.GetLength() available that takes the dimension parameter. For all other uses, For i = 0 to Array.Length - 1 is better and probably the fastest option.

like image 76
dotNET Avatar answered Oct 28 '22 20:10

dotNET