C# Collections

Collection is a data type which holds a group of data. Commonly used collections include ArrayList, Queue, Stack, the generic classes such as List, Dictionary are also collections.

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

The generic collection List:
using System.Collections.Generic;
using System.Windows.Forms;
List<string> lst = new List<string>();
lst.Add("New York");
lst.Add("Houston");
List<int> lst = new List<int> {4,9,18,3};
MessageBox.Show(lst2.Count.ToString()); //4

Dictionary<T,T> is another useful collection, similar to the hash arrays of other programming languages, but with flexable length.
using System.Collections.Generic;
Dictionary<string, int> d = new Dictionary<string, int> {
{"Jacobs",28},{"Mary",26}
};
Dictionary<string, int> d2 = new Dictionary<string, int>();
d2.Add("Mary",26);
d2.Add("Chris",32);
d2["John"] = 23;



endmemo.com © 2017  Terms of Use | Home