Is there anyway to have a typed array where each element is a different object?
-
2If you're typing your array, why would you want to store different types? Can you explain a bit more? You can always use a non-typed list and store whatever you want.Esteban Araya– Esteban Araya2009-07-13 01:27:50 +00:00Commented Jul 13, 2009 at 1:27
-
1Are you perhaps asking about a tuple? A tuple is a data structure like Tuple<int, string, double> -- that is, there are three elements in the tuple, and they are an int, a string and a double. There will be a standard tuple type added to the framework in the next version.Eric Lippert– Eric Lippert2009-07-13 01:35:21 +00:00Commented Jul 13, 2009 at 1:35
-
I'm not sure what you're asking.Steven Sudit– Steven Sudit2009-07-13 05:19:47 +00:00Commented Jul 13, 2009 at 5:19
Add a comment
|
1 Answer
Yep, you could have all the generic type be some super class or interface all of all of your objects share.
Like you might have Apple and Orange inherit from Fruit so you could put them both in a List<Fruit>.
Alternatively, if the objects are all of totally different types you could make a List<object>, but that would undermine the value of generics quite a bit. (For Harvey) Such as fewer casts, better intelliprompt, linq, and better performance if you are using collections of structs such as ints and doubles.