From the course: Learning C#

Defining properties - C# Tutorial

From the course: Learning C#

Defining properties

- [Instructor] Now that we've learned how to control and define access to the number of variables and methods within our class definition using access modifiers, we're going to take a look at how we can safely expose them to other parts of our application. Now, in a previous example, we saw how to do this using public methods but it's a bit cumbersome to define methods just for accessing data fields. Methods should be doing real work instead of just setting and getting values. So instead, we're going to expose our object's data using properties, which are sort of a combination of member variables and methods. So let's take a look at an example. So here in our Classes folder, I'm going to open up the Properties folder and open up the Program file, as well as our Book class. So here we have the Book class example that we've been working with and it has three private data members. Name, author and pagecount. Now, suppose I wanted to give access to the _name field. So I can do that by declaring something called a public property as if it were a member variable. So what I'm going to do is use the word public, 'cause I want this to be public. I'm going to give it a type string and then I'll give it a Name, which in this case is Name. And I'm going to declare it with curly braces. Inside the curly braces, I'm going to add code that's called a getter and a setter. So I'm going to use the get keyword and for get, I'm going to return _name. And then for set, I'm going to set _name equal to this keyword named value and the value is something that's automatically passed in to each property when you set the value. So it doesn't need to be defined anywhere, it's just like the magical value that gets passed in. So these getters and setters are responsible for returning the value, as well as setting the value. And in this case, what we're doing is we're setting and getting this private property _name, which in this case is called a backing field because this public property controls access to this private field that's in our class and as I mentioned, in the setter case, the value keyword's a special parameter. You don't need to declare this. It just happens to show up. In fact, this form of getting and setting property values is so common, it actually has a shorthand way of writing it called an expression-bodied property. So let's do this for the other two private fields. And I'm going to collapse it down so I have more room. So what I'm going to do is define a public string property called Author. And then for the get case, I'm going to use the arrow notation, so it's equals and then greater than. That basically means just return, not pagecount, I want to return author and then for set, same thing. I'm going to set _author equal to value. So this saves me from having to write those curly braces. It's just a shorthand way of doing this and then I'll do the same thing for pagecount. And that's an integer. So I'll have public int Pagecount. And then get will just simply return pagecount. And then for set, we'll have pagecount equals to value. If I wanted to make one of these properties read only or write only, I can just remove the setter or the getter. So for example, if I take this set expression out of the page count, that would make it read only. So because now there's only a getter, there's no setter. So let me undo that. So one of the great uses for properties is to create what are called computed properties, which get their values from other properties and expressions. So let's go ahead and create a property called description, which returns a string description of the book and it's only going to have a getter, so it's read only. So once again, I'll make a public string. We'll call it Description. And the getter is going to return an interpolated string and I'm going to refer to the other properties in here. Name by Author and it's going to have Pagecount pages. And so for the last example, let's take a look at something called autogenerated properties. So these are properties that don't have backing fields in the class. So they not only control the data access but they actually hold the data as well. So let's add two properties. So I'm going to add a public string called ISBN. And I'll add another one called public decimal for the Price. And then in each of these definitions, I'm going to add a very simple get; and set; and I'll do the same thing for Price. Just get and set. So this will autogenerate the getter and the setter for me. So there's no internal data fields associated with these properties. They just hold the data directly. So let's go to our program and now try these out. So I already have the code to create the book. And so now I have some code here to print out the name and the description and then I have some code to set the ISBN and the price and then print those out and then you can see that I have some code that changes the name and the Pagecount and then prints out the description, name and Pagecount again. So let's go ahead and save this. And we'll go to the Properties folder and open this in the integrated terminal. And then let's run this. All right, so the results are basically what we expect. So here is the name. And the initial description, followed by the ISBN number and the updated price information. And then here's the updated description after we change the name and the page count and then we have just the name and then just the page count. So properties are probably one of my favorite features of C# because they make it really easy to control how class data is accessed and they're really easy to read and understand. You can read more about properties at this link in the documentation and I suggest taking some time here and trying out your own experiments with the example code that we have here.

Contents