Code to add different types of datatype in ArrayList and then to get specific types of datatypes

Amatya
Posted by Amatya under C# category on | Points: 40 | Views : 854
System.Collections.ArrayList AllType = new System.Collections.ArrayList();

// Add numbers to the list
AllType.Add(7);
AllType.Add(21);

// Add strings to the list
AllType.Add("Hello");
AllType.Add("This is going to be a problem");
//for retrieving string value
ArrayList stringArray = new ArrayList();
foreach (string s in AllType.OfType<string>())
{
stringArray.Add(s);
}
//for retrieving int value
ArrayList intArray = new ArrayList();
foreach (int i in AllType.OfType<int>())
{
intArray.Add(i);
}
OR
string[] stringList = AllType.OfType<string>().ToArray();
Int32[] intList = AllType.OfType<Int32>().ToArray();

Comments or Responses

Login to post response