C# ArrayList

ArrayList is similar to array in other programming languages such as php.

using System.Collections;
ArrayList al = new ArrayList();
al.Add("Monday");
al.Add("Tuesday");
al.Add("Wednesday");
foreach (string str in al)
{
...
}

Count the ArrayList length.
int num = al.Count; //3

Check whether the ArrayList has a specific element.
bool b = al.Contains("Monday"); //true

Check an element's position in the ArrayList.
int num = al.IndexOf("Tuesday"); //1

Some other functions of ArrayList.
al.Insert("Thursday"); //void
al.Remove("Monday"); //void

ArrayList is similar to List. But since the data type is specified in List initialization, while ArrayList is not, ArrayList may has casting errors.
using System.Collections;
ArrayList al = new ArrayList();
al.Add(1);
al.Add("Monday"); //will cause casting error later



endmemo.com © 2017  Terms of Use | Home