We start learning about object oriented programming in this part of my C# tutorial. We’ll cover Classes, Methods, Fields, Constructors, Static Classes, Methods and Fields, Structs and Nullable Types. I’m going to cover OOP over a few videos to make sure everyone completely understands everything.
All of the code follows the video below. For best results, print it out and take notes in your own words and then ask me if you have any questions.
If you like videos like this consider donating a $1 on Patreon.
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 |
---------- Program.cs ---------- using System; namespace CSharpTutA.cs { class Program { static void Main(string[] args) { // Create a Rectangle Rectangle rect1; // Add values to it and run the Area method rect1.length = 200; rect1.width = 50; Console.WriteLine("Area of rect1 : {0}", rect1.Area()); // Use a constructor to create a Rectangle Rectangle rect2 = new Rectangle(100, 40); // If you assign one Rectangle to another // you are setting the values and not // creating a reference rect2 = rect1; rect1.length = 33; Console.WriteLine("rect2.length : {0}", rect2.length); // ----- OBJECT ORIENTED PROGRAMMING ----- // A class models real world objects by // defining their attributes (fields) and // capabilities (methods) // Then unlike with structs you can // inherit from a class and create more // specific subclass types // Add a class Project -> Add Class // Create an Animal object // You could also assign values like // fox.name = "Red" Animal fox = new Animal() { name = "Red", sound = "Raaaw" }; // Call the static method Console.WriteLine("# of Animals {0}", Animal.GetNumAnimals()); // You can also create static utility // classes Project -> Add Class Console.WriteLine("Area of Rectangle : {0}", ShapeMath.GetArea("rectangle", 5, 6)); // ----- NULLABLE TYPES ----- // Data types by default cannot have a // value of null. Often null is needed // when you are working with databases // and you can create a null type by // adding a ? to the definition int? randNum = null; // Check for null if(randNum == null) { Console.WriteLine("randNum is null"); } // Another check for null if (!randNum.HasValue) { Console.WriteLine("randNum is null"); } Console.ReadLine(); } // ----- STRUCTS ----- // A struct is a user defined type that // contain multiple fields and methods struct Rectangle { public double length; public double width; // You can create a constructor method // that will set the values for fields public Rectangle(double l=1, double w=1) { length = l; width = w; } public double Area() { return length * width; } } } } ---------- Animal.cs ---------- using System; namespace CSharpTutA.cs { class Animal { // Attributes (fields) that all Animals have // public means can be directly changed // after an object has been created public string name; public string sound; // A constructor sets default values for // fields when an object is created // This is the default constructor if no // parameters are sent public Animal() { name = "No Name"; sound = "No Sound"; numOfAnimals++; } // You can create additonal constructors // but since we are definig defaults you // don't have to public Animal(string name = "No Name") { // This refers to this objects name // instead of the name passed into // the constructor this.name = name; this.sound = "No Sound"; numOfAnimals++; } public Animal(string name = "No Name", string sound = "No Sound") { this.name = name; this.sound = sound; numOfAnimals++; } // Capabilities (methods) that all Animals have public void MakeSound() { Console.WriteLine("{0} says {1}", name, sound); } // static fields and methods belong to the class // A static field has the same value for all // objects of the Animal type static int numOfAnimals = 0; public static int GetNumAnimals() { return numOfAnimals; } } } ---------- ShapeMath.cs ---------- using System; public static class ShapeMath { // This class can only contain static methods // and constant values public static double GetArea(string shape = "", double length1 = 0, double length2 = 0) { if (String.Equals("Rectangle", shape, StringComparison.OrdinalIgnoreCase)) { return length1 * length2; } else if (String.Equals("Triangle", shape, StringComparison.OrdinalIgnoreCase)) { return length1 * (length2 / 2); } else if (String.Equals("Circle", shape, StringComparison.OrdinalIgnoreCase)) { return 3.14159 * Math.Pow(length1, 2); } else { return -1; } } } |
Leave a Reply