Auto-Properties4:12 with Jeremy McLain
Simple properties can be written even more succinctly as automatically implemented properties.
In the previous video we learned how to make classes more flexible 0:00 with properties. 0:03 Properties can also be used to perform better encapsulation. 0:05 Look at the _location property we just wrote. 0:09 We can remove either the getter or the setter portion of the property. 0:11 Think about what would happen if we removed the getter. 0:15 This means other classes can set the value of the location, but they can't read it. 0:19 Methods and other properties within the Invader class 0:25 could still read the location by using the private field directly. 0:27 What if we remove the setter instead? 0:33 Now other classes can get the _location, but they can't set it. 0:36 The _location field isn't read only, so methods and other properties 0:40 within the Invader class can still set the _location by setting it directly. 0:44 Pretty nifty? 0:50 Well, sorta. 0:51 Once there's a property that gets and 0:54 sets a field it's generally bad practice to access the field directly. 0:55 You should just ignore the fact that the field is there and 1:01 try to just use the property whenever possible. 1:04 Otherwise, having two different ways to change the value of a field can be 1:08 confusing. 1:11 The property is almost always the one you want to use. 1:13 We still want to be able to restrict read and write access to the property though. 1:16 Instead of deleting the getter and 1:20 setter, we can use the private access modifier instead. 1:22 If we put the private keyword here on the get, 1:27 Then other classes will only be able to set the _location. 1:31 Methods and 1:36 other properties in the Invader class will still be able to get it though. 1:36 It's fairly rare to have a private getter and a public setter. 1:41 On the other hand, having a private setter and a public getter is very common. 1:45 Now other classes can get the _location but they can't set it. 1:50 Methods and properties in the Invader class can still use the setter though. 1:54 Most properties do exactly what this code here does. 1:59 They just get and set the value of the field. 2:05 In these cases, we can further simplify this code. 2:10 We can replace what we have here with get set. 2:14 We can also delete the field entirely. 2:20 And we can put this on a single line. 2:24 In C# this is called an auto-property because it doesn't have 2:31 a backing field and it automatically implements a getter and a setter. 2:35 This is nice because now instead of having both a property and 2:40 a field, we just have the property. 2:44 It's still used the same. 2:47 And we can still set the access to the getter and setter individually. 2:48 We want this one private. 2:53 If at some time in the future we need the more verbose property, 2:56 then we can always expand this back to its original version. 3:00 And we won't lose any of the benefits that a property provides. 3:03 Now we have a _location property that allows other classes to get the Invader's 3:07 location on the map, and allows the Invader class to update its own _location. 3:10 It's been a while since we last compiled. 3:15 So, let's do that just to make sure we haven't introduce any 3:17 errors into the code. 3:20 So, let's open up the console and type in our compile commands, 3:21 mcs -out:TreehouseDefense.exe *.cs. 3:28 Looks like we do have an error here. 3:33 Let's see here. 3:34 TreehouseDefense.Invader.Location cannot be used in this context 3:35 because the set accessor is inaccessible. 3:40 I think I know what's going on here. 3:43 We made the setter private. 3:45 So now, back in the Game class, 3:48 we can't set the location any more from the Game class. 3:51 We don't need this code anyway, so we'll just clear it out. 3:56 Lets compile again, just make sure that fixes it. 4:00 There we go. 4:03 No compile errors. 4:04 In the next video we'll finish coding the Invader class by writing a method for 4:06 moving the invader down the path. 4:10
You need to sign up for Treehouse in order to download course files.Sign up