I cover a bunch of cool things you can do with C# in this tutorial. We’ll learn about IEnumerable, Indexer, Enumerator, Operator Overloading, Custom Casting, Anonymous Types and more.
For best results take notes on the cheat sheet provided below as you watch and leave any questions you have.
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
// ---------- Animal.cs ---------- namespace CSharpTutA.cs { class Animal { public string Name { get; set; } public Animal(string name = "No Name") { Name = name; } } } // ---------- AnimalFarm.cs ---------- using System.Collections; using System.Collections.Generic; namespace CSharpTutA.cs { // IEnumerable provides for iteration // over a collection class AnimalFarm : IEnumerable { // Holds list of Animals private List<Animal> animalList = new List<Animal>(); public AnimalFarm(List<Animal> animalList) { this.animalList = animalList; } public AnimalFarm() { } // Indexer for AnimalFarm created with this[] public Animal this[int index] { get { return (Animal)animalList[index]; } set { animalList.Insert(index, value); } } // Returns the number of values in the // collection public int Count { get{ return animalList.Count; } } // Returns an enumerator that is used to // iterate through the collection IEnumerator IEnumerable.GetEnumerator() { return animalList.GetEnumerator(); } } } // ---------- Box.cs ---------- using System; namespace CSharpTutA.cs { class Box { public double Length { get; set; } public double Width { get; set; } public double Breadth { get; set; } public Box() :this(1,1,1) { } public Box(double l, double w, double b) { Length = l; Width = w; Breadth = b; } public static Box operator +(Box box1, Box box2) { Box newBox = new Box() { Length = box1.Length + box2.Length, Width = box1.Width + box2.Width, Breadth = box1.Breadth + box2.Breadth }; return newBox; } // Through operator overloading you can // allow users to interact with your // custom objects in interesting ways // You can overload +, -, *, /, %, !, // ==, !=, >, <, >=, <=, ++ and -- public static Box operator -(Box box1, Box box2) { Box newBox = new Box() { Length = box1.Length - box2.Length, Width = box1.Width - box2.Width, Breadth = box1.Breadth - box2.Breadth }; return newBox; } public static bool operator ==(Box box1, Box box2) { if((box1.Length == box2.Length) && (box1.Width == box2.Width) && (box1.Breadth == box2.Breadth)) { return true; } return false; } public static bool operator !=(Box box1, Box box2) { if ((box1.Length != box2.Length) || (box1.Width != box2.Width) || (box1.Breadth != box2.Breadth)) { return true; } return false; } // You define how your object is converted // into a string by overridding ToString public override string ToString() { return String.Format("Box with Height : {0} Width : {1} and Breadth : {2}", Length, Width, Breadth); } // You can convert from a Box into an // int like this even though this // wouldn't make sense public static explicit operator int(Box b) { return (int)(b.Length + b.Width + b.Breadth) / 3; } // Convert from an int to a Box public static implicit operator Box(int i) { // Return a box with the lengths all // set to the int passed return new Box(i, i, i); } } } // ---------- Program.cs ---------- using System; using System.Linq; using System.Collections.Generic; namespace CSharpTutA.cs { // Indexers allow you to access items // like arrays class Program { static void Main(string[] args) { // Create an AnimalFarm object AnimalFarm myAnimals = new AnimalFarm(); // Add Animals myAnimals[0] = new Animal("Wilbur"); myAnimals[1] = new Animal("Templeton"); myAnimals[2] = new Animal("Gander"); myAnimals[3] = new Animal("Charlotte"); foreach(Animal i in myAnimals) { Console.WriteLine(i.Name); } // Testing operator overloading Box box1 = new Box(2, 3, 4); Box box2 = new Box(5,6,7); Box box3 = box1 + box2; // Converts the box to a string with // ToString Console.WriteLine($"Box 3 : {box3}"); Console.WriteLine($"Box Int : {(int)box3}"); // Convert an int into a Box Box box4 = (Box)4; Console.WriteLine($"Box 4 : {(Box)4}"); // Sometimes you want to build a simple // class that contains fields and // Anonymous types are great for that var shopkins = new { Name = "Shopkins", Price = 4.99 }; Console.WriteLine("{0} cost ${1}", shopkins.Name, shopkins.Price); // Anonymous types can also be stored // in an array var toyArray = new[] { new { Name = "Yo-Kai Pack", Price = 4.99 }, new { Name = "Legos", Price = 9.99 } }; // You can loop through the array foreach(var item in toyArray) { Console.WriteLine("{0} costs ${1}", item.Name, item.Price); } Console.WriteLine(); Console.ReadLine(); } } } |
Leave a Reply