- understand the advantages of using
List<T>
instead ofArrayList
- understand how to use the
List<T>
class - have an overview of the available collection types
- know how to implement a custom collection with the help of
IEnumerator<T>
andIEnumerable<T>
Similar data can often be handled more efficiently when stored and manipulated as a collection MSDocs. You can find a guide for choosing the right type of collection at: https://docs.microsoft.com/en-us/dotnet/standard/collections/ . The document also discusses the algorithmic complexity of various operations.
Assignment
Sample code available – Check the “StandardCollections” Sample
-
Create a new project with the name “StandardCollections”
-
Add the following method in the “Program” class and call it from the Main method
private static void ArrayListExample() { var words = new ArrayList(); words.Add("melon"); words.Add("avocado"); string first = (string)words[0]; //int first = (int)words[0]; }
-
Add the following method in the “Program” class and call it from the Main method
private static void ListExample() { // New string-typed list var words = new List<string>(); words.Add("melon"); words.Add("avocado"); words.AddRange(new[] { "banana", "plum" }); // Insert at start words.Insert(0, "lemon"); // Insert at start words.InsertRange(0, new[] { "peach", "nashi" }); words.Remove("melon"); // Remove the 4th element words.RemoveAt(3); // Remove first 2 elements words.RemoveRange(0, 2); // Remove all strings starting in 'n': words.RemoveAll(x => x.StartsWith("n")); for (var i=0; i<words.Count; i++) { Console.WriteLine(words[i]); } foreach (var word in words) { Console.WriteLine(word); } }
-
Add the following “Person” class
internal class Person { #region Properties public string Name { get; set; } public int Age { get; set; } #endregion public Person(string name, int age) { Name = name; Age = age; } }
-
Add the following method in the
Program
class and call it from the Main methodprivate static void ListPersonExample() { var personList = new List<Person>(); var rnd = new Random(); for (var i = 0; i < 10; i++) { personList.Add(new Person("Persoana " + i, rnd.Next(100))); } //Which interface is needed for Array.Sort(personList) foreach (var p in personList) //equivalent to foreach (Person p in personList) Console.WriteLine(p); }
-
SortedList<TKey, TValue> and many others: link
Assignment
Sample code available – Check the “CustomCollections” Sample
-
Add the following
PersonCollection
classinternal class PersonCollection :IEnumerable<Person> { private Person[] _personArray; //Indexer [int] public Person this[int index] { get { return _personArray[index]; } set { _personArray[index] = value; } } //Indexer [string] public Person this[string name] { get { return _personArray.FirstOrDefault(x=>x.Name == name); } } public int Length { get { return _personArray.Length; } } public PersonCollection() { _personArray = new [] { new Person("name1", 1), new Person("name2", 2), new Person("name3", 3) }; } public IEnumerator<Person> GetEnumerator() { return new PersonEnumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
-
Add the following
PersonEnumerator
classinternal class PersonEnumerator : IEnumerator<Person> { private int _nIndex; private PersonCollection _personCollection; public PersonEnumerator(PersonCollection personCollection) { _personCollection = personCollection; _nIndex = -1; } public bool MoveNext() { _nIndex++; return _nIndex < _personCollection.Length; } public void Reset() { _nIndex = -1; } public Person Current { get { return _personCollection[_nIndex]; } } object IEnumerator.Current { get { return Current; } } public void Dispose() { } }
-
Add the following method in the
Program
class and call it from the Main methodprivate static void PersonCollectionExample() { var personList = new PersonCollection(); foreach (var p in personList) Console.WriteLine(p); }