In this part of my C# tutorial we cover public, private, protected, constants, read-only fields, constructors, setters, getters, properties and more on static. I’m taking my time to make sure everyone completely understands object oriented programming in C#. We are getting near the end of the basic syntax and will start doing fun stuff soon.
For best results print the code below and write out your own notes in your own words and use that as a cheat sheet.
If you like videos like this consider donating a $1 on Patreon, or a simple like helps a lot as well 🙂
[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 |
// ----- Animal.cs ----- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // We cover public, private, protected, constants, // read-only fields, constructors, setters, getters, // properties and more on static namespace CSharpTutA.cs { class Animal { // Define fields that are protected with private // Private fields can only be accessed by // methods in the class and they are not // accessible by subclasses // protected fields can only be accessed by // methods in the class and by subclasses private string name; private string sound; // You can define constants public const string SHELTER = "Derek's Home for Animals"; // You can define read-only fields that are set // at runtime in constructors, but then can't // be changed public readonly int idNum; // Method (Capabilities) public void MakeSound() { Console.WriteLine("{0} says {1}", name, sound); } // Default constructor public Animal() :this("No Name", "No Sound") { } // Constructor called if only name is passed // this passes the parameters to the next // constructor public Animal(string name) : this(name, "No Sound") { } // Constructor that receives parameters public Animal(string name, string sound) { SetName(name); Sound = sound; // Increment the number of animals // property NumOfAnimals = 1; // Define the read-only value which is // the same for all Animals Random rnd = new Random(); idNum = rnd.Next(1, 2147483640); } // Setters (Mutators) protect the fields // from receiving bad data public void SetName(string name) { // Any checks if any character in the string // is a number and if so returns true // Since we won't allow numbers we will // protect our data if (!name.Any(char.IsDigit)) { this.name = name; } else { // We have this duplicated code because // everything isn't a property this.name = "No Name"; Console.WriteLine("Name can't contain numbers"); } } // Getters (Accessors) can provide output // aside from the value stored public string GetName() { return name; } // The preferred way to define getters and // setters is through properties public string Sound { get { return sound; } set { // value is assigned the value passed in if (value.Length > 10) { sound = "No Sound"; Console.WriteLine("Sound is too long"); } else { sound = value; } } } // You can have the getters and setters // generated for you like this and also // set the default value public string Owner { get; set; } = "No Owner"; // You can also define static properties public static int numOfAnimals = 0; public static int NumOfAnimals { get { return numOfAnimals; } set { numOfAnimals += value; } } } } // ----- Program.cs ----- using System; namespace CSharpTutA.cs { class Program { static void Main(string[] args) { Animal cat = new Animal(); // Call the setter cat.SetName("Whiskers"); // Set the property cat.Sound = "Meow"; Console.WriteLine("The cat is named {0} and says {1}", cat.GetName(), cat.Sound); // Test auto generated getters and setters cat.Owner = "Derek"; Console.WriteLine("{0} owner is {1}", cat.GetName(), cat.Owner); // Get the read-only id number Console.WriteLine("{0} shelter id is {1}", cat.GetName(), cat.idNum); // Test static property Console.WriteLine("# of Animals : {0}", Animal.NumOfAnimals); Console.ReadLine(); } } } |
Leave a Reply