Lecture thumbnail 0:00 / 0:00 I don’t want to start sounding like unbox therapy or some other YouTube show, but today I’ve got something
weird and wonderful to show you, and that is the idea of array backed properties.
Now why would you want the array backed properties in the first place?
Well, let me give you an example.
Let’s suppose you’re making a computer game now, this computer game, let’s say you’ve got a bunch
of creatures roaming the grounds.
Now for each creature, you define some of these statistics which describe this creature, like, for
example, intelligence or strength or agility, something like that.
So you define a property for each one of them.
You have strength and you have agility and you have intelligence.
And this works fine.
But at some point you want to report, for example, the average statistic of a creature.
So how do you do it?
Typically now the way you can do this is you can of course, write a getter.
So you can write public double average stat, write a getter for it.
And here, of course, we would have to take the strength plus agility plus intelligence divided by
three.
And that’s our result.
Now, this works if you only have a single getter, and if you don’t envisage yourself adding additional
statistics later on.
But if you do, then you want some way of iterating the class’s properties.
And this problem of how do you iterate the class’s properties has a simple and elegant solution.
If you think about the properties here, you can see that all of the properties have an automatically
generated backing field, but it’s really up to you.
You as a programmer can decide what the backing fields actually are, so nothing prevents you from having
a single array type backing field for every single property of type int, for example.
So here I can make a private integer array called stats, which is going to be an array of three elements.
And then what I can do is I can rewrite each of the properties, I can rewrite their getters and setters
to actually project into the array.
So it’s a bit like a proxy property in a way, because for the getter you return, for example, stats
at zero.
And similarly for the setter, you say stats at zero is equal to the value.
So let’s write it like this and you would do the same for the other properties as well.
Let’s actually streamline them using C-sharp seven syntax.
So this is how you implement an array back property.
But why would you do this in the first place?
Well, look at this getter, for example.
This getter is now a lot more stable in the sense that if you add additional stats, you don’t have
to rewrite this getter because this getter devolves to a very simple call to stats dot average.
That’s all that you have to do.
And notice that because you have a backing store which is an array, you get the benefits of Linq.
Everything that can be called an enumerable of INT is available on your stats array.
So for example, if you want to find the minimum statistic of a creature or the maximum statistic or
the sum of all the statistics, you can do this automatically.
You can do this like in an eye blink and you don’t have to write any code yourself.
Or because Linq has already implemented those aggregate operators for you and in a similar fashion,
you can define other things like, for example, let’s suppose that you want a creature to expose only
these fields and you want it to be enumerable.
In this case, all you have to do is you just define an enumerable of int you out, enter in resharper
and generate all the appropriate members.
And here all you have to do is you have to return stats dot and then is as enumerable dot get enumerator.
That’s all that you have to do in order to actually expose every single property in an enumerable way.
So essentially we’re not talking so much about the core iterator pattern here, but we are talking about
the idea of iteration only this time around.
It’s not an iteration of an array which is exposed directly, but rather it is an iteration of the different
properties.
And yes, we expose it in this particular location in a way because we expose the enumerator, but we
don’t give out the array.
The array is still a private field.
It’s simply exposed as a bunch of properties that you can access.
And in a similar way you can improve the situation.
Like, for example, some people might say, well, hold on, this is a magic number.
Why would you have a magic number in here?
And suddenly you can fix that as well.
So for example, you can have a private int strength equals zero.
So this can actually be a const, even const or a static.
And then here what you can do is you can start doing things like Stats at Strength, which reads very
nice.
It’s very kind of, you know, if you read it left to right in English, you get the statistics for
the strength, you set the statistics for strength to a given value.
So it’s really cool.
It’s really just, you know, a sensible approach to how you would want to build this.
And in a similar fashion you can expose, for example, an indexer to a creature if you want to get
the different values.
So you would write something like public int.
This int index and you can write a getter here, and the getter would return, for example, the value.
So once again we return stats at index.
We could get the setter as well.
So stats at index is equal to value.
And in addition, there is another benefit to this, which means that you have an observable collection
so you can have this collection exposed as an observable collection and thereby you can monitor the
changes to the values and react to them in, you know, the typical reactive way, you know, reactive
extensions and all that.
So we just did a very simple trick here.
We chose a backing field for several properties to be a single array, but the benefits are huge.
And this is what I wanted to show you.
Play Play Play Play Stop Start Play information alert