Using ArrayList
-
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
-
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
-
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).
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.
-
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.
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 anArrayList
, you can useforeach
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
-
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.
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 fromobject
. On the other hand, if you insert value types (int
s,double
s,DateTime
s and so on) in theArrayList
, the performance penalty is big because when you put a value type in anobject
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 anobject
to a value type (when getting an item from theArrayList
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 fromCollectionBase
is that the compiler does type checking for you. You usually can't assign anobject
to a typed variable without casting, but the other way around is not true: you can assign a variable of any type to aobject
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
-
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).
And this can save you time... TypedCollectionGenerator http://kristopherjohnson.net/cgi-bin/twiki/view/KJ/TypedCollectionGenerator[^] Kevin
-
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
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 ));