By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

Is it possible to put different datatypes in the same List<>?

P: n/a
Hello

List<Tis said to be more powerful than ArrayLists but if you have
something like this:

List<intmylst = new List<>;
myList.Add("Joe");
myList.Add(25);

the list doesn't seem to accept the name "Joe".

How do you get a List<to accept different datatype if possible?

JB
Oct 28 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"JB" <JB@discussions.microsoft.comwrote in message
news:70**********************************@microsof t.com...
Hello

List<Tis said to be more powerful than ArrayLists but if you have
something like this:

List<intmylst = new List<>;
myList.Add("Joe");
myList.Add(25);

the list doesn't seem to accept the name "Joe".

How do you get a List<to accept different datatype if possible?
List<objectmyList = new List<object>();
myList.Add("Joe");
myList.Add(25);

Oct 28 '08 #2

P: n/a
JB wrote:
List<Tis said to be more powerful than ArrayLists but if you have
something like this:

List<intmylst = new List<>;
myList.Add("Joe");
myList.Add(25);

the list doesn't seem to accept the name "Joe".

How do you get a List<to accept different datatype if possible?
The old ArrayList or List<object>.

But but but - maybe you should reconsider your object model - it
looks very weird. Maybe you want a List<Personwhere Person
has Name and Age properties.

Arne
Oct 28 '08 #3

P: n/a
If you are using List<objectand adding value types to the list, you
are doing lot of boxing and un-boxing.
Arne already pointed out that "you should reconsider your object model "

Thanks & Regards,
Ashutosh

John Vottero wrote:
>
"JB" <JB@discussions.microsoft.comwrote in message
news:70**********************************@microsof t.com...
>Hello

List<Tis said to be more powerful than ArrayLists but if you have
something like this:

List<intmylst = new List<>;
myList.Add("Joe");
myList.Add(25);

the list doesn't seem to accept the name "Joe".

How do you get a List<to accept different datatype if possible?

List<objectmyList = new List<object>();
myList.Add("Joe");
myList.Add(25);
Oct 28 '08 #4

P: n/a
JB wrote:
Hello

List<Tis said to be more powerful than ArrayLists but if you have
something like this:

List<intmylst = new List<>;
myList.Add("Joe");
myList.Add(25);

the list doesn't seem to accept the name "Joe".

How do you get a List<to accept different datatype if possible?

JB
You use the most specific data type that is common for the data types
that you want to put in the list.

As mentioned in the thread, List<Objectworks the same way as an
ArrayList. If you really need a list of object references, you should
rather use List<Objectthan ArrayList. That way it's clear that you
actually indended to use that kind of list, and not that you just used
an ArrayList because you didn't know better.

In most cases, you should be able to find a common data type that is
more specific than Object. For example if you want to put instances of
System.Windows.Controls.Button and System.Windows.Controls.Label in the
same list, you can use a List<System.Windows.Controls.Control>.

--
Göran Andersson
_____
http://www.guffa.com
Oct 28 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.