-4

Is it possible, to do something like that:

ArrayList arl = new ArrayList();
arl.Add(1);
arl.Add("test");

int[,] tab = new int[4, 4];
init(tab);

arl.Add(tab);

Is it possible to contain objects of various types (Like in JavaScript or Lua)? (C#)

6
  • Does this give a compile error? ArrayList contains objects Commented Mar 20, 2013 at 9:38
  • try to run to see how C# compiler react Commented Mar 20, 2013 at 9:38
  • Did you tried at least once? I don't see any effort here.. Commented Mar 20, 2013 at 9:39
  • Look at this already discussed [Objects in ArrayList] [1]: stackoverflow.com/questions/3367524/… Commented Mar 20, 2013 at 9:40
  • 1
    You should ask yourself why you need to do this. Rarely would you need to in a statically typed language. Commented Mar 20, 2013 at 9:41

5 Answers 5

1

Yes, ArrayList contains elements of type object so this means you can put everything in it. When retrieving it again, you can then case it back to its original type. Of course, due to this casting it's less efficient.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes it is possible. ArrayList stores a collection of the type object meaning you can insert any .NET type.

You should really use List<T>. For example:

 List<int> listIntegers = new List<int>();
 listIntegers.Add(1);

You could also use List<object> however you will have to unbox all of the items in the list. Which potentially may incur performance issues.

2 Comments

This would appear to be a random downvoter. I've just had the two questions I asked here downvoted for no particular reason.. :/
I have had it before, you can send a mail to stack overflow to act on this.
0

You can have diffent types in a ArrayList try this:

var ary = new ArrayList();

ary.Add("some string");
ary.Add(23.4);
ary.Add(new StringBuilder());
ary.Add("some other string");

You can then query it like this:

string[] strings = ary.OfType<string>();
StringBuilder[] stringBuilders = ary.OfType<StringBuilder>(); 

Comments

0

As for your question, yes ArrayList can contain various object types.
It is generally recommended by Microsoft to use List<T> instead, to prevent the need of boxing and unboxing to Type object when using the same types.

If you have a recurring pattern of types it might be more helpful (and faster) to define a custom class:

protected class arl_items
{
   public string item1 {get; set;};
   public int item2 {get; set;};
   public int[4,4] item3 {get; set;};
}

and then go:

List<arl_items> arl = new List<arl_items>();

But if there is no pattern to your value-types you can as well use ArrayList, because creating List<object> would be meaningless, as they are the same.

Just btw. i prefer using List<object> over ArrayList, but that is only my personal preference

2 Comments

..perhaps clarify your use of a generic List<object> here. I would downvote your answer but I am not sure if you are saying an ArrayList and List<object> are essentially identical (which, they are), or you're saying to use a List<object> instead of an ArrayList (which is pointless).
@SimonWhitehead thanks, i wasn't aware that i mixed my personal preferences into the answer too much
-1

Yes it can since it does not do any type checking, it can sometimes be faster than a List<T> when working with reference types, though it is generally not recommended to use it when you have a perfectly fine type safe alternative.

Any type that inherits from object can be stored in an ArrayList. Whenever you access an item in an ArrayList you must be careful to cast it to the correct type or else you will get a compiler error.

I believe ArrayList is an old relic from the days before generic types in .Net

4 Comments

What? ... you're incorrect. In fact, when casting objects back out it will be slower due to runtime type checking..
Depends on what you're storing in the arraylist, for reference types ArrayList is actually a little faster (by a negligible amount to be fair), tested by adding reference type to arraylist over 1000000 iterations and confirmed. On the other hand, adding to a List<int> is significantly faster than adding to an ArrayList.
I think I've read your answer wrong. The way I interpreted it, you were saying that an ArrayList always performs faster than a List<class>.. is that not what you meant?
No that's not what i meant, i suppose i could have worded it a little differently, i'll edit my post to clear some things up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.