Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store multiple data types in an ArrayList using C#?

Tags:

c#

In C#, can I store multiple data types in an ArrayList? Like;

myArrayList.Add(false);
myArrayList.Add("abc");
myarrayList.Add(26);
myArrayList.Add(obj);

I know i can make a DataTable or a class for it.

But, please let me know: is this possible? And if so, what are it's De-merits of being a collection class?

like image 316
Purnesh Avatar asked Jan 16 '23 04:01

Purnesh


1 Answers

Yes you can store like this

ArrayList aa = new ArrayList();
aa.Add(false);
aa.Add(1);
aa.Add("Name");

ArrayList belongs to the days that C# didn't have generics. It's deprecated in favor of List. You shouldn't use ArrayList in new code that targets .NET >= 2.0 unless you have to interface with an old API that uses it.

ArrayList vs List<> in C#

like image 146
andy Avatar answered Jan 29 '23 14:01

andy