 Hey everybody, this is Brian, and this is the 15th C-Sharp tutorial. Let's make a new console application. And let's make a new class. And let's just call this class, hmm. Hmm, I'm at a loss of what to call this. Let's call it wizard, just because I'm out of names. So we have our wizard class here. And what we're going to discuss today are getters and setters. Now, if you're used to other languages, you've seen things like this. Public, int, get age. And then, you know, you want a private, meaning it's only used inside this class, we'll call it mH. So you want to get the age, so we'll say return mH. And then if you want to set the age, you would say something like public void set age. And then let's do int and age. Well, this seems like a lot of typing, isn't it? I think it is. mH, hmm. Probably help if we had an access type here. And as you can see, it's kind of clunky and error prone. There we go, mH equal nH. And then you can do whatever you want within these things. Well, Microsoft added what's called the get and set function. So what we're going to do here is we're going to make a property. So we're going to say public, int, age. And then brackets. Now, you notice how we didn't put these parentheses at the end, because this is not a function, this is a property. Then you're going to say get. And notice how it has the get word in there. A code block. And then set. And a code block. That does pretty much the same thing. Now, you notice how get's kind of complaining and saying, not all code paths return a value. Because what it's expecting is this, return mH. And then you can say, so that we'll say mH equal value. Well, where's value coming from? Value is a virtual variable, if there is such a thing, that basically the Microsoft.net framework is throwing in. So whatever you want to set the property, the value passed to it is literally value. So this right here takes place of these two functions. Now, why would you use a property? Well, property, very simple, because it acts like a variable. Let's say, gets or sets the age. There we go. And then let's actually create our wizard here. How many times have you said that in life, let's create a wizard. And we'll say, see wizard dot age. Now, you notice how we have the get and set, but we don't need them. Because we can just say age equals, and we'll say 12. Because, well, he's kind of a childish wizard. Let's do a console read. And let's do a console dot write. Write line, and we'll say, see wizard dot age. And let's actually go back in here. Let's delete this stuff out, because we really don't need it. So let's review really quickly. We have a property, see public and age. And in our property, we can get or set. Now, get returns the value, and you say return m age. Because this is essentially a code block, a function. You can do any sort of computation you want in here. Same thing with set. So that's a code block. You can do anything you want in here. But the purpose of get and set is get will return a value, and set will set the value. So let's actually save and run this. Sure enough, it says 12. So what's a common usage of a property? Well, let's say we want a read-only property. So let's just say public int, and we'll say read-only age. Don't get thrown by the name. We could name this anything we want. And let's just copy this, paste that. Now you notice how there's no set block. There's just a get. And that's because, well, it's read-only. We say read-only age equals 22. And we try to run that. It says build errors. What's the problem here? It says cannot be assigned to. It is read-only. That is how you protect your internal variables. Now don't get thrown by the read-only age. We could very easily just call this bunny slippers if we wanted to. I mean, you can call it anything you want. It's a property. But in practice, you know, instead of calling something bunny slippers, you should give it a descriptive name such as age. Notice how it's highlighting it because it's saying you cannot set. You can only get. Now, when you set something, you're using the equal sign age equals 12. So that's calling the set portion. When you get it, you just, well, you just say wizard.age. So you're getting it. In other words, age is going to return. Set is going to set it. Very simple. I know it sounds a little confusing, but really all you need to understand is that you are making a property. That property is exposed to the outside world because this variable, we call this very fragile. We don't want this exposed to the outside world because, you know, just raw chaos could happen. For example, we could say if, not here, sorry, that's in the get, if value is greater than 100. Well, nobody lives past 100, right? Wrong answer. I know some people do. But actually, let's say if it's past 200, because nobody lives to be 200 old, then we'll say m age equals 200. Say that's the upper limit. Else, we will just set the value. So this is the power of a property. This is the level of complexity you can really get is just astounding. I mean, you could actually just pass this off to another class or a function and do some huge mathematical algorithm. So what we're doing is saying get just simply returns the age. Set, if the value they're trying to set is over 200, we're going to say age is 200, because that's the maximum we're going to allow. Else, we'll just set the value. So let's actually try to do this. Let's say 300. And let's see what happens here. Run this. And sure enough, the maximum age returned is 200. So as you can see, properties are very simple yet very powerful. And they're a way of protecting your internal variables inside your class. Because, well, if you just let users run a muck, you get weird things like, you know, Microsoft Corporation. Anyways, this is Brian. Thank you for watching. I hope you found this educational and entertaining. And keep up on the feedback. And one thing I should probably mention before I go is if you are watching these tutorials, you can go out on my website, www.voidrealms.com, voidrealms.com. And you notice the word void that we've been talking about. I mean, yeah, my website returns nothing. Anyways, go to tutorials and then C-sharp. And I've been uploading all these, including the source code. So like you can just, you know, go out here and you can just download the source code for each tutorial and watch the video while you're out there. No, there's no subscription fees or anything like that. I do ask for donations. If, however, you don't want to donate, you don't have to. You can just click to download. The only reason I ask for donations is because, well, I put a lot of stuff up here and some businesses use this to train their employees. And if you guys find this worth your while and you're actually using this for business functions, just donate what you feel it's worth. Otherwise, just have fun.