472,334 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 software developers and data experts.

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


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.
Nov 21 '05 #1
1 57393
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" <bl****@newsgroup.nospam> wrote in message
news:fa********************************@4ax.com...
|
| 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.
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: elziko | last post by:
I have a four dimensional array which I am trying to flatten into a single dimension array: Dim Array4D(10,10,10,10) as Single I can create my...
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So...
9
by: OpticTygre | last post by:
If I have a multidimensional array, such as array1(5, 3), and I want to find the length of the first section (the 5), can I just use array1.length,...
7
by: Peter | last post by:
I want to create a multidemensional arraylist. Seeing as they don't exist I was wondering if there is a way to create a class that works like one....
4
by: sal | last post by:
Greets, All Converting array formula to work with datatables/dataset tia sal I finally completed a formula I was working on, see working code...
8
by: zulander | last post by:
is there a way to find out if the element exist ? dim myarray() as string dim mytxt as string mytxt ="Superman1,Superman2,Superman3,Superman4"...
3
by: Jerry West | last post by:
I'd like to get the upper bound index of an integer array. I've tried the following: Dim i as Integer Dim arrayIng() as Integer i =...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at...
3
by: jac130 | last post by:
the program runs, and user is prompted via inputbox to enter an integer-this is the size of the array, then the user fills the array with that many...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.