tags:

views:

56

answers:

3

Can we add in the list object any type of value ?

Like if we

List<string> str=new List<string>();

then only string value can be added to list.But I want to add any type of value in the list like string,decimal.

A: 

Then use ArrayList

Srinivas Reddy Thatiparthy
Do not use ArrayList. Use List<object>.
Eric Lippert
A: 

You can use the non generic equivalent of List<T> which is System.Collections.ArrayList.

var list = new System.Collections.ArrayList();
list.Add("test");
list.Add(1);

Or you could use List<object>.

Markus Dulghier
Do **NOT** use ArrayList. Among other reasons, it does not work on Silverlight. Always use List<object> if you need a list that can hold anything. (However, it would probably be better to revisit the design decision which requires strings and numbers to be in the same list; that's a very, very bad code smell.)
Eric Lippert
could you explain/provide a link for "Among other reasons"
Srinivas Reddy Thatiparthy
A: 

Then you can use none generic type of List. Use ArrayList or even you can use List<object>

Incognito