473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 57796
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 one dimensional array to be big enough to contain...
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 I tried the following: #include<stdio.h>...
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, or would that return 15 (5 * 3) instead of 5? ...
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. I basically want to use it like this Dim...
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 below. I would like to change this code so it...
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" myarray=mytxt.split(",") ...
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 = arrayIng.GetUpperBound This doesn't work. It seems...
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 "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
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 values...but as the user enters the values, i need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.