In this part of my C# tutorial we’ll explore many collections available with C#. We’ll look at ArrayLists, Dictionaries, Queues, and Stacks. We’ll test out most every function available with them.
For best results take notes on the heavily commented code provided below as you watch and leave any questions you have. This is the book students tell me is the best for learning C#.
If you like videos like this consider donating a $1 on Patreon.
[googleplusone]
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
using System; using System.Linq; // Used for ArrayLists using System.Collections; // Used for Dictionary using System.Collections.Generic; namespace CSharpTutA.cs { class Program { static void Main(string[] args) { // Collections can resize unlike arrays // #region provides a way to collapse // long blocks of code // ---------- ARRAYLISTS ---------- #region ArrayList Code // ArrayLists are resizable arrays // that can hold multiple data types ArrayList aList = new ArrayList(); aList.Add("Bob"); aList.Add(40); // Number of items in list Console.WriteLine("Count: {0}", aList.Count); // The capacity automatically increases // as items are added Console.WriteLine("Capacity: {0}", aList.Capacity); ArrayList aList2 = new ArrayList(); // Add an object array aList2.AddRange(new object[] { "Mike", "Sally", "Egg" }); // Add 1 array list to another aList.AddRange(aList2); // You can sort the list if the types // are the same aList2.Sort(); aList2.Reverse(); // Insert at the 2nd position aList2.Insert(1, "Turkey"); // Get the 1st 2 items ArrayList range = aList2.GetRange(0, 2); /* // Remove the first item aList2.RemoveAt(0); // Remove the 1st 2 items aList2.RemoveRange(0, 2); */ // Search for a match starting at the provided // index. You can also find the last index // with LastIndexOf Console.WriteLine("Turkey Index : {0}", aList2.IndexOf("Turkey", 0)); // Cycle through the list foreach (object o in range) { Console.WriteLine(o); } // Convert an ArrayList into a string array string[] myArray = (string[])aList2.ToArray(typeof(string)); // Convert a string array into an ArrayList string[] customers = { "Bob", "Sally", "Sue" }; ArrayList custArrayList = new ArrayList(); custArrayList.AddRange(customers); #endregion // ---------- DICTIONARIES ---------- #region Dictionary Code // Dictionaries store key value pairs // To create them define the data // type for the key and the value Dictionary<string, string> superheroes = new Dictionary<string, string>(); superheroes.Add("Clark Kent", "Superman"); superheroes.Add("Bruce Wayne", "Batman"); superheroes.Add("Barry West", "Flash"); // Remove a key / value superheroes.Remove("Barry West"); // Number of keys Console.WriteLine("Count : {0}", superheroes.Count); // Check if a key is present Console.WriteLine("Clark Kent : {0}", superheroes.ContainsKey("Clark Kent")); // Get the value for the key and store it // in a string superheroes.TryGetValue("Clark Kent", out string test); Console.WriteLine($"Clark Kent : {test}"); // Cycle through key value pairs foreach(KeyValuePair<string, string> item in superheroes) { Console.WriteLine("{0} : {1}", item.Key, item.Value); } // Clear a dictionary superheroes.Clear(); #endregion // ---------- QUEUES ---------- #region Queue Code // Queue 1st in 1st Out Collection // Create a Queue Queue queue = new Queue(); // Add values queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); // Is item in queue Console.WriteLine("1 in Queue : {0}", queue.Contains(1)); // Remove 1st item from queue Console.WriteLine("Remove 1 : {0}", queue.Dequeue()); // Look at 1st item but don't remove Console.WriteLine("Peek 1 : {0}", queue.Peek()); // Copy queue to array object[] numArray = queue.ToArray(); // Print array Console.WriteLine(string.Join(",", numArray)); // Print queue items foreach (object o in queue) { Console.WriteLine($"Queue : {o}"); } // Clear the queue queue.Clear(); #endregion // ---------- STACKS ---------- #region Queue Code // Stack Last in 1st Out Collection // Create a stack Stack stack = new Stack(); // Put items on the stack stack.Push(1); stack.Push(2); stack.Push(3); // Get but don't remove item Console.WriteLine("Peek 1 : {0}", stack.Peek()); // Remove item Console.WriteLine("Pop 1 : {0}", stack.Pop()); // Does item exist on stack Console.WriteLine("Contain 1 : {0}", stack.Contains(1)); // Copy stack to array object[] numArray2 = stack.ToArray(); // Print array Console.WriteLine(string.Join(",", numArray2)); // Print the stack foreach (object o in stack) { Console.WriteLine($"Stack : {o}"); } // Clear stack #endregion Console.ReadLine(); } } } |
Leave a Reply