Can I have fixed typed ArrayList in C#, just like C++?

arraylist, c#

Solution

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.

Problem

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.

Original source