I have an ArrayList
which contains fixed type of objects.
However everytime I need to extract an object a particular index, I need to typecast it to my user defined type from object type.
Is there a way in C# to declare ArrayList
of fixed types just like Java and C++, or is there a work around to avoid the typecasting everytime?
Edit:
I apologize I forgot mentioning that I require the datastructure to be thread-safe, which List
is not. Otherwise I would have just used a normal Array
. But I want to save myself from the effort of explicitly locking and unlocking while writing the array.
So I thought of using ArrayList
, synchronize it, but it requires typecasting every time.
You could use a List. The List class takes advantage of generics to make a strongly typed collection.
To use, just call new List< Type you want to use >() like this:
List<string> myStringList = new List<string>();
MSDN has a quick article on some ways you can make collections thread safe.
Take a look at System.Collections.Generic.List
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With