Page 97 - CSharp/C#
P. 97

// Adding and changing data
         people["John"] = 40;    // Overwriting values this way is ok
         people.Add("John", 40); // Throws ArgumentException since "John" already exists

         // Iterating through contents
         foreach(KeyValuePair<string, int> person in people)
         {
             Console.WriteLine("Name={0}, Age={1}", person.Key, person.Value);
         }

         foreach(string name in people.Keys)
         {
             Console.WriteLine("Name={0}", name);
         }

         foreach(int age in people.Values)
         {
             Console.WriteLine("Age={0}", age);
         }



        Duplicate key when using collection


        initialization




         var people = new Dictionary<string, int>
         {
             { "John", 30 }, {"Mary", 35}, {"Jack", 40}, {"Jack", 40}
         }; // throws ArgumentException since "Jack" already exists


        Stack



         // Initialize a stack object of integers
         var stack = new Stack<int>();

         // add some data
         stack.Push(3);
         stack.Push(5);
         stack.Push(8);

         // elements are stored with "first in, last out" order.
         // stack from top to bottom is: 8, 5, 3

         // We can use peek to see the top element of the stack.
         Console.WriteLine(stack.Peek()); // prints 8

         // Pop removes the top element of the stack and returns it.
         Console.WriteLine(stack.Pop()); // prints 8
         Console.WriteLine(stack.Pop()); // prints 5
         Console.WriteLine(stack.Pop()); // prints 3


        LinkedList







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