ArrayList can take all kinds of objects. You may add few integers, then some strings, and even some complex types including DataSet, DataTable etc.
Create an ArrayList and add 4 different type of items into it...
ArrayList myList = new ArrayList();
myList.Add ("DotNetSpider"); // Add a string.
myList.Add(1032); // Add an integer
myList.Add( DateTime.Now ); // Add current time.
myList.Add( new DataTable() ); // Add a datatable
Now iterate through the ArrayList and read one by one. We added 4 items of different types.
So, after retrieving check the type of each item and display it.
// Now retrieve items one by one.
foreach (object item in myList)
{
if ( item.GetType() == typeof(string) )
{
Console.WriteLine( "Item is a string..." );
}
else if ( item.GetType() == typeof(int) )
{
Console.WriteLine( "Item is a integer..." );
}
else if ( item.GetType() == typeof(DateTime) )
{
Console.WriteLine( "Item is a DateTime object..." );
}
else if ( item.GetType() == typeof(DataTable) )
{
Console.WriteLine( "Item is a DataTable..." );
}
}
The above code produces the following output:
Item is a string...
Item is a integer...
Item is a DateTime object...
Item is a DataTable...