Page 96 - CSharp/C#
P. 96

// output:
         // one
         // two

         // exchange the element on the first position
         // note that all collections start with the index 0
         myArray[0] = "something else";


         // enumerate through the array again
         foreach(var item in myArray)
         {
             Console.WriteLine(item);
         }

         // output:
         // something else
         // two


        List


        List<T> is a list of a given type. Items can be added, inserted, removed and addressed by index.


         using System.Collections.Generic;

         var list = new List<int>() { 1, 2, 3, 4, 5 };
         list.Add(6);
         Console.WriteLine(list.Count); // 6
         list.RemoveAt(3);
         Console.WriteLine(list.Count); // 5
         Console.WriteLine(list[3]);    // 5


        List<T> can be thought of as an array that you can resize. Enumerating over the collection in order
        is quick, as is access to individual elements via their index. To access elements based on some
        aspect of their value, or some other key, a Dictionary<T> will provide faster lookup.


        Dictionary


        Dictionary<TKey, TValue> is a map. For a given key there can be one value in the dictionary.


         using System.Collections.Generic;

         var people = new Dictionary<string, int>
         {
             { "John", 30 }, {"Mary", 35}, {"Jack", 40}
         };

         // Reading data
         Console.WriteLine(people["John"]); // 30
         Console.WriteLine(people["George"]); // throws KeyNotFoundException

         int age;
         if (people.TryGetValue("Mary", out age))
         {
             Console.WriteLine(age); // 35
         }



        https://riptutorial.com/                                                                               42
   91   92   93   94   95   96   97   98   99   100   101