As we start our journeys to understanding the basics of programming and development, one of the things we learn about are variables. A variable is simply the declaration of various data types that define and point to a storage area. We use variables in various places in our code, for reference, manipulation, and other operations. One of those places are in classes. Classes give us a format or a blueprint to use as the design for the objects we create. As we are creating these blueprints we define our data types and methods or operations this object will have. Theses are our member definitions and what brings use to topic of the day.
Lets take a look at some code:
We first define a class named Person, within it some member definitions. The first of those definitions are member variables, we declare a string and int called "name" and "age" respectively. Next, we create a override to the default constructor, it requires a string and int as arguments and it uses those to set the public member variables to the supplied values. Lastly we create a member method that writes a string to the console with our person's name and age injected.
This all works fine and well however, let's take a peak at what could happen if someone was consuming our code. Keep in mind this is the simplest of examples meant to show a point.
First we create a new object of the Person class type and we pass Suzy for the name and 23 for the age. Added to our constructor, is the first call to Talk() so we do not have to call it, as you can see in the command prompt console it displays with Suzy and 23 correctly. Next we access the "name" member variable off of p1 and change it to Joe. We can do this because we defined it as public which sets it's access level to full throughout the program or a consumer. You can see it show up on intelliSense as well:
Using Properties and Backing Fields, referred to as "Getters and Setters" is the way around this and the proper way to control / encapsulate your variables. Using encapsulation (Getters & Setters) you can restrict access, perform various checks, or many other operations on the data as your class requires before allowing or denying the modification. There are a several ways to do this, let's discuss the automagic way. Auto-Implemented, when we use this method the compiler uses some syntactic sugar for us and creates a backing field (private variable) so it saves us a bit of typing. The Getter & Setter control the access to those backing fields and is up to us to define that. In the example below, I left the Getter to public (default) and I set the Setter to private so that it can only be accessed through the class. Take note the default here is opposite of what you get when you define variables and methods, the default for those are private unless defined otherwise.
We can also create a method in our class that modifies the values if we like, but again, we are controlling when and where these values are able to be accessed and modified.
Or we could create a Factory method to do it all for us.
So you see, we have much more control over our variables this way and can perform many other operations in a controlled manner. If we don't want them accessed at all we simply only implement a Getter and no we code without the worry of something getting changed in our class that would have an undesired effect (BUG!).