0

Is it possible to store integer, strings and user-defined class objects in one arraylist like

ArrayList a=new ArrayList();

class Demo {}
Demo d=new Demo();

a.Add(12);

a.Add("Faizal Sardar Khan");

a.Add(d);

If it all it is possible, then how to access the elements stored(how to cast 'em out)? If not, then is there any way out to implement this?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Nikunj Vats
  • 33
  • 1
  • 6
  • Doesn't the code in the question already do what you ask? – David Heffernan Nov 04 '13 at 15:53
  • 4
    Yes, but you shouldn't. You should change your design to use strongly-typed classes, then use generic collections. – SLaks Nov 04 '13 at 15:53
  • Yes, it can, but I'd recommend `List`, since `ArrayList` is pretty much obsolete. Why do you want to do this, though? Storing totally different types of objects together rarely happens. And how do you plan to use it? (that should help answer your question of how to access them) – Tim S. Nov 04 '13 at 15:54

5 Answers5

2

You can use is keyword to check type of objects:

if (a[0] is String) /* do something */
if (a[1] is Demo) /* something other */
Zergatul
  • 1,957
  • 1
  • 18
  • 28
1

You may not care exactly what object is there, so you can just treat them as objects.

foreach(var o in myList)
   Console.WriteLine(o);

If you know the types exactly, you can cast.

Dog dog = (Dog)myList[26];
dog.Bark();

If you want different behavior depending on type and you don't know where they are, you can just check what they are:

// Reference types:
Dog dog = myList[i] as Dog;
if (dog != null)
   dog.Bark();
Cow cow = myList[i] as Cow;
if (cow != null)
   cow.Moo();
// Value types:
if (myList[i] is int)
   DoSomethingWith((int)myList[i]);

Regardless of how and why, these are just variations of the same design smell...

Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
0

Yes, that is certainly possible. It's also possible via a List<Object>. As for casting, that depends. How will you know what is where in your list? But to finish out your sample:

int twelve = (int)a[0];
string s = (string)a[1];
Demo e = (Demo)a[2];

It would nice to know what your end goal is here, because I expect you're trying to use the ArrayList as something that it's not really intended to be, and we may be able to suggest a much better way.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • I basically want to store different type of data in one single Collection. Since in List we need to specify beforehand what type of elements would be stored in it, so I thought ArrayList might be an option. – Nikunj Vats Nov 04 '13 at 17:22
  • @NikunjVats But _why_ do you want to do this? For what purpose? This desire is almost always a symptom of a design flaw elsewhere that we can help correct. – Joel Coehoorn Nov 04 '13 at 17:56
0

It's possible (if not advisable) to insert different types of objects into an ArrayList because it just holds objects.

How to get them out again is tricky. One way would be to just have a bunch of if conditions to check if the object is of a certain type, and then cast it to that type.

Rik
  • 28,507
  • 14
  • 48
  • 67
0

You gotta check the type of an element before you can use it:

Object anything = list[0];
if (anything is Int32) {
   var number = (Int32) anything;
} else if (anyting is String) {
   var text = (String) anything;
} else {
   if (Object.ReferenceEquals(null, anything)) {
       throw new NotSupportedException("The element is NULL.");
   } else {
       throw new NotSupportedException("Unexpected type of the element \"" + anything.GetType().Name + "\".");
   }
}
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159