1

Is there anyway to have a typed array where each element is a different object?

3
  • 2
    If 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. Commented Jul 13, 2009 at 1:27
  • 1
    Are 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. Commented Jul 13, 2009 at 1:35
  • I'm not sure what you're asking. Commented Jul 13, 2009 at 5:19

1 Answer 1

2

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.

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

Comments

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.