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. Using ArrayList

Using ArrayList

Scheduled Pinned Locked Moved C#
csharptutorialquestionlearning
7 Posts 7 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.
  • M Offline
    M Offline
    MarkMokris
    wrote on last edited by
    #1

    IN my C# apps, I use ArrayList's to store many types of objects. Of course when I pull anything out of any ArrayList I have to employ a cast. For example: ArrayList listOfStrings(); : String myString = (String)listOfStrings[3]; I end of doing so much casting! Is that typical? Can I avoid all the casting? Or is there some fundamental technique using ArrayLists that I am missing? Thanks! Mark Mokris

    G R 2 Replies Last reply
    0
    • M MarkMokris

      IN my C# apps, I use ArrayList's to store many types of objects. Of course when I pull anything out of any ArrayList I have to employ a cast. For example: ArrayList listOfStrings(); : String myString = (String)listOfStrings[3]; I end of doing so much casting! Is that typical? Can I avoid all the casting? Or is there some fundamental technique using ArrayLists that I am missing? Thanks! Mark Mokris

      G Offline
      G Offline
      gnjunge
      wrote on last edited by
      #2

      You want to create your own strong typed collection by deriving from System.Collections.CollectionBase class. Check the MSDN for CollectionBase to see how to implement this (override Add, Remove, this[] etc methods).

      D K 2 Replies Last reply
      0
      • G gnjunge

        You want to create your own strong typed collection by deriving from System.Collections.CollectionBase class. Check the MSDN for CollectionBase to see how to implement this (override Add, Remove, this[] etc methods).

        D Offline
        D Offline
        Daniel1324
        wrote on last edited by
        #3

        I think the question was is there anything wrong with all of the casting. I also employ the arraylist for the same purpose, so I'd like to hear from the C# gurus too.

        M L 2 Replies Last reply
        0
        • D Daniel1324

          I think the question was is there anything wrong with all of the casting. I also employ the arraylist for the same purpose, so I'd like to hear from the C# gurus too.

          M Offline
          M Offline
          Matt Gerrans
          wrote on last edited by
          #4

          Well, generics will help if you're using VS 2005, but with the current version, you're stuck with the cast or using the as syntax. Of course if you are iterating over the items in an ArrayList, you can use foreach and that is nicer.

          foreach( string s in arrayListOfStrings )
          Console.WriteLine( "The list contains {0}", s );

          Matt Gerrans -- modified at 22:59 Thursday 25th August, 2005

          1 Reply Last reply
          0
          • D Daniel1324

            I think the question was is there anything wrong with all of the casting. I also employ the arraylist for the same purpose, so I'd like to hear from the C# gurus too.

            L Offline
            L Offline
            Luis Alonso Ramos
            wrote on last edited by
            #5

            It depends on what types of objects you are inserting in the ArrayList. If you are inserting reference types (basically any object) there's a very slight, almost negligible, performance penalty in casting to and from object. On the other hand, if you insert value types (ints, doubles, DateTimes and so on) in the ArrayList, the performance penalty is big because when you put a value type in an object variable, the compiler has to create a dummy object to contain the value and insert a reference to that object (a process called boxing). When you cast an object to a value type (when getting an item from the ArrayList for example) the process is called unboxing and is the exact opposite: go get the object and take the value from inside it. An advantage of using specialized collections derived from CollectionBase is that the compiler does type checking for you. You usually can't assign an object to a typed variable without casting, but the other way around is not true: you can assign a variable of any type to a object variable. With a specialized collection, you can't add items of other types without the compiler complaining. -- LuisR


            Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

            The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005 -- modified at 23:13 Thursday 25th August, 2005

            1 Reply Last reply
            0
            • G gnjunge

              You want to create your own strong typed collection by deriving from System.Collections.CollectionBase class. Check the MSDN for CollectionBase to see how to implement this (override Add, Remove, this[] etc methods).

              K Offline
              K Offline
              Kevin McFarlane
              wrote on last edited by
              #6

              And this can save you time... TypedCollectionGenerator http://kristopherjohnson.net/cgi-bin/twiki/view/KJ/TypedCollectionGenerator[^] Kevin

              1 Reply Last reply
              0
              • M MarkMokris

                IN my C# apps, I use ArrayList's to store many types of objects. Of course when I pull anything out of any ArrayList I have to employ a cast. For example: ArrayList listOfStrings(); : String myString = (String)listOfStrings[3]; I end of doing so much casting! Is that typical? Can I avoid all the casting? Or is there some fundamental technique using ArrayLists that I am missing? Thanks! Mark Mokris

                R Offline
                R Offline
                radic feng
                wrote on last edited by
                #7

                My experience is adding objects with the same type to one arraylist. if casting is needed, for instance, to a string array. you can use this statement: string[] myString = ( string[] )listOfStrings.toArray( typeof(string ));

                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