Array.Length vs Array.GetUpperBound(0) -- any real differences?

S

Samuel R. Neff

Are there any differences between using Array.Length and
Array.GetUpperBound(0) on a one-dimensional array?

We have a team of developers and most people use Array.Length but one
developer uses GetUpperBound(0). I'd like the code to all be
consistent but would like to know if there is any other reason I can
provide to justify using only Array.Length instead of
GetUpperBound(0).

I read that Array.Length has special meaning to the compiler in that a
loop over an array in C# will bypass bounds checks within the loop
since the compiler knows ahead of time that all accesses will be
valid. Does VB.NET provide this same optimization? Does it apply
only to Length or does it also apply to GetUpperBounds(0).

Thanks,

Sam

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 
J

Jay B. Harlow [MVP - Outlook]

Samuel,
| Are there any differences between using Array.Length and
| Array.GetUpperBound(0) on a one-dimensional array?
Yes!

Array.Length returns the length of the array (number of elements) you need
to subtract 1 from it to get the UpperBound.

Array.GetUpperBound(0) returns the upper bound of the array, you can use it
as is...

I normally use Array.Length, as other collections have either a Length or
Count property. However I have considered changing to GetUpperBound to avoid
subtracting 1 from the length.

IMHO Which you use in VB.NET is personal preference, however I would be
mindful of the "Oddball Solution" smell. The oddball solution smell is
described in Joshua Kerievsky's book "Refactoring to Patterns". An oddball
solution is when you normally do something one way in a project
(Array.Length) but in one section of code you do the same thing (get number
of elements in the array) a different way (Array.GetUpperBound).

Hope this helps
Jay

|
| Are there any differences between using Array.Length and
| Array.GetUpperBound(0) on a one-dimensional array?
|
| We have a team of developers and most people use Array.Length but one
| developer uses GetUpperBound(0). I'd like the code to all be
| consistent but would like to know if there is any other reason I can
| provide to justify using only Array.Length instead of
| GetUpperBound(0).
|
| I read that Array.Length has special meaning to the compiler in that a
| loop over an array in C# will bypass bounds checks within the loop
| since the compiler knows ahead of time that all accesses will be
| valid. Does VB.NET provide this same optimization? Does it apply
| only to Length or does it also apply to GetUpperBounds(0).
|
| Thanks,
|
| Sam
|
| B-Line is now hiring one Washington D.C. area VB.NET
| developer for WinForms + WebServices position.
| Seaking mid to senior level developer. For
| information or to apply e-mail resume to
| sam_blinex_com.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top