Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Array.getLength() vs Array.getUpperBound()

Array.getLength() vs Array.getUpperBound()

Scheduled Pinned Locked Moved C#
visual-studiodata-structuresquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    C NewBe
    wrote on last edited by
    #1

    Is there a difference between the two?

    J G D C 4 Replies Last reply
    0
    • C C NewBe

      Is there a difference between the two?

      J Offline
      J Offline
      jchigg2000
      wrote on last edited by
      #2

      getLength returns the total number of elements and getUpperBound returns the highest index number (getLength - 1) in most cases. getUpperBound can also be used on arrays of 2 or more dimensions to get the upper index of a dimension.

      1 Reply Last reply
      0
      • C C NewBe

        Is there a difference between the two?

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        C++NewBe wrote:

        Is there a difference between the two?

        Yes. GetLength returns the number of items in the specified dimension. GetUpperBound returns the index of the last item in the specified dimension. For a one dimensional zero based array (which is the usual) that holds 10 items, GetLength(0) returns 10, while GetUpperBound(0) returns 9.

        --- Year happy = new Year(2007);

        1 Reply Last reply
        0
        • C C NewBe

          Is there a difference between the two?

          D Offline
          D Offline
          Dustin Metzgar
          wrote on last edited by
          #4

          Yes, GetUpperBound is used to get the boundaries of different dimensions of an array. Call GetUpperBound(0) to get the upper bound of the first dimension, GetUpperBound(1) for the second dimension and so on. It's in case you're using a 2-dimensional (or higher) array. Also, GetUpperBound returns the highest index of that array instead of the number of values in that array. So, whereas you would do for (int i = 0; i < arr.Length; i++) for length, you would do this for the upper bound: for (int i = 0; i <= arr.GetUpperBound(0); i++)

          1 Reply Last reply
          0
          • C C NewBe

            Is there a difference between the two?

            C Offline
            C Offline
            C NewBe
            wrote on last edited by
            #5

            Thanks for the explanation guys....

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups