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