PC Review


Reply
Thread Tools Rate Thread

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

 
 
Samuel R. Neff
Guest
Posts: n/a
 
      31st Mar 2005

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.
 
Reply With Quote
 
 
 
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      31st Mar 2005
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

"Samuel R. Neff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
|
| 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.


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array formula to show differences in 2 lists? goofy11 Microsoft Excel Worksheet Functions 1 5th Mar 2008 05:54 PM
Redimming an array dynamically assigned from range (how to redim first dimension of a 2-D array? /or/ reverse the original array order) Keith R Microsoft Excel Programming 3 13th Nov 2007 04:08 PM
string array differences =?ISO-8859-1?Q?G=F6ran_Andersson?= Microsoft VB .NET 3 15th Feb 2007 06:57 AM
meaning of : IF(Switch; Average(array A, array B); array A) =?Utf-8?B?RFhBVA==?= Microsoft Excel Worksheet Functions 1 24th Oct 2006 06:11 PM
array = null or array.length = 0 Rene Microsoft C# .NET 1 31st Aug 2004 04:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:36 PM.