Properties4:22 with Jeremy McLain
Accessor methods can be written more efficiently using C# properties. Properties have benefits over accessor methods.
When it comes to fields, the rule to code by is to make all fields private, and 0:00 then write public getters and setters that access it indirectly. 0:05 We just learned how to write accessor methods in the previous video. 0:09 You can imagine this forces you to write a lot of additional code. 0:14 Two methods for every field. 0:17 Fortunately, C# has some syntactic sugar that makes it much easier to write 0:20 accessors like these. 0:24 It's called a property. 0:26 We can replace these two methods with the property. 0:28 I'll write the property down here so 0:30 we can see the differences between methods and properties. 0:33 First, we give the property an access modifier. 0:37 I'll make it public. 0:39 Then we give it it a type. 0:41 The type should match the type of the field. 0:42 Now we give it the name. 0:48 I'll name it location. 0:50 Inside the curly braces, we'll write our getter and our setter. 0:52 The getter will do exactly what the get location method up here did, 0:57 it will just return the field. 1:01 The setter will do exactly what the set location did, 1:06 it will set the location field. 1:10 Properties have the same benefits that we got from having these two methods. 1:14 They have a getter and a setter. 1:19 They correspond to these two methods. 1:22 In the case of the setter, the value parameter is implied. 1:25 Value is just a variable that stores the value being assigned to the field. 1:29 You can think of these as two separate methods. 1:34 In fact, behind the scenes, 1:37 properties look identical to these two methods we wrote up here. 1:39 They're used differently, though. 1:43 Let's go to main and compare using methods with using properties. 1:44 Let's clear out this code first. 1:49 Now let's make an invader. 1:53 So let's say Invader. 1:55 Equals Invader. 1:58 Let's also make a new map location. 2:04 So map location, I just call it location, equals new map location 2:06 and pass in that would be at 0, 0 and pass in the map. 2:14 There we go. 2:22 [BLANK] So 2:22 to use the invaders Set Location Method to set the location, 2:31 we call the Set Location Method and pass in this location. 2:35 When using a property instead, we can just assign location a new value. 2:41 So we can just say invader.location = location. 2:47 This is not setting the location field directly, though. 2:53 Instead, it's calling the setter inside the location property, right here. 2:56 We can even add additional code here. 3:03 So say we wanted to print something every time the location changed. 3:05 So we could say, system.console.rightline. 3:10 Location changed. 3:16 So the same goes for getters. 3:19 We can call the property getter in the same way we would read a field. 3:22 So we could say location = invader.location. 3:27 So this gets the location from the invader and 3:31 sets it back into the local variable location. 3:37 Not very practical, but this is just for demonstration purposes. 3:42 So now that we have a property we can delete these two methods here. 3:46 And we don't need to do anything in the setter other than just set the location. 3:51 Pretty nifty. 3:57 As you can see, 3:58 properties support the encapsulation principles of object oriented programming. 3:59 They allow us to hide how an invader's location is represented 4:04 in the class internally. 4:07 This allows us to make changes to the class without 4:09 affecting the users of the class. 4:12 We'll see many more examples of how to use properties as we go forward. 4:14 We'll also learn how to use properties to do even better encapsulation. 4:18
You need to sign up for Treehouse in order to download course files.Sign up