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. UpperBound of Dynamic array

UpperBound of Dynamic array

Scheduled Pinned Locked Moved C#
csharpdata-structuresquestion
9 Posts 6 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.
  • E Offline
    E Offline
    Enobong Adahada
    wrote on last edited by
    #1

    Hello, Please I want to get the upperbound of a dynamic array, is there a function in c# to achive this? thanks.

    S OriginalGriffO P 3 Replies Last reply
    0
    • E Enobong Adahada

      Hello, Please I want to get the upperbound of a dynamic array, is there a function in c# to achive this? thanks.

      S Offline
      S Offline
      Sathesh Sakthivel
      wrote on last edited by
      #2

      Dynamic Array[^]

      Sathesh. Blessed is the season which engages the whole world in a conspiracy of love.

      1 Reply Last reply
      0
      • E Enobong Adahada

        Hello, Please I want to get the upperbound of a dynamic array, is there a function in c# to achive this? thanks.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        You can find how many elements are in an array with the Length property. C# doesn't have dynamic arrays - they are all fixed when you declare them, but you can fake it by creating a new array and copying the old elements to it. This is not a dynamic array however, as only the references you specifically change have the new size.

                int\[\] orig = new int\[\] { 1, 2, 3, 4, 5, 6 };
                int\[\] copy = orig;
                int\[\] temp = new int\[10\];
                for (int i = 0; i < copy.Length; i++)
                    {
                    temp\[i\] = copy\[i\];
                    }
                copy = temp;
        

        "copy" now refers to an array of ints with ten elements, but "orig" tsill referes to an array of six. If you want a dynamic array like structure, you would be better off using a List<T> which has a ToArray method if you need it.

        Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        R E 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          You can find how many elements are in an array with the Length property. C# doesn't have dynamic arrays - they are all fixed when you declare them, but you can fake it by creating a new array and copying the old elements to it. This is not a dynamic array however, as only the references you specifically change have the new size.

                  int\[\] orig = new int\[\] { 1, 2, 3, 4, 5, 6 };
                  int\[\] copy = orig;
                  int\[\] temp = new int\[10\];
                  for (int i = 0; i < copy.Length; i++)
                      {
                      temp\[i\] = copy\[i\];
                      }
                  copy = temp;
          

          "copy" now refers to an array of ints with ten elements, but "orig" tsill referes to an array of six. If you want a dynamic array like structure, you would be better off using a List<T> which has a ToArray method if you need it.

          Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

          R Offline
          R Offline
          riced
          wrote on last edited by
          #4

          OriginalGriff wrote:

          If you want a dynamic array like structure, you would be better off using a List

          Or switch to VB? :laugh:

          Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

          OriginalGriffO K 2 Replies Last reply
          0
          • R riced

            OriginalGriff wrote:

            If you want a dynamic array like structure, you would be better off using a List

            Or switch to VB? :laugh:

            Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            riced wrote:

            Or switch to VB?

            Wash your keyboard out with soap! I would never recommend anyone switch to VB. Well, except Estate Agents. :laugh:

            Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • R riced

              OriginalGriff wrote:

              If you want a dynamic array like structure, you would be better off using a List

              Or switch to VB? :laugh:

              Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

              K Offline
              K Offline
              Keith Barrow
              wrote on last edited by
              #6

              The smiley saved you from a 1 vote :-)

              ragnaroknrol The Internet is For Porn[^]
              Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

              1 Reply Last reply
              0
              • E Enobong Adahada

                Hello, Please I want to get the upperbound of a dynamic array, is there a function in c# to achive this? thanks.

                P Online
                P Online
                PIEBALDconsult
                wrote on last edited by
                #7

                Where did you find a dynamic array? :confused:

                R 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Where did you find a dynamic array? :confused:

                  R Offline
                  R Offline
                  riced
                  wrote on last edited by
                  #8

                  He looked under the VB sofa and there it was. :laugh:

                  Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    You can find how many elements are in an array with the Length property. C# doesn't have dynamic arrays - they are all fixed when you declare them, but you can fake it by creating a new array and copying the old elements to it. This is not a dynamic array however, as only the references you specifically change have the new size.

                            int\[\] orig = new int\[\] { 1, 2, 3, 4, 5, 6 };
                            int\[\] copy = orig;
                            int\[\] temp = new int\[10\];
                            for (int i = 0; i < copy.Length; i++)
                                {
                                temp\[i\] = copy\[i\];
                                }
                            copy = temp;
                    

                    "copy" now refers to an array of ints with ten elements, but "orig" tsill referes to an array of six. If you want a dynamic array like structure, you would be better off using a List<T> which has a ToArray method if you need it.

                    Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

                    E Offline
                    E Offline
                    Enobong Adahada
                    wrote on last edited by
                    #9

                    Thanks all, I discovered that when using the foreach... loop to access an array, you don't need to know the dimension before hand. it works great. thanks again for your contributions

                    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