Getters, Setters and Properties - Learn Object-Oriented Programming in C#
Getters, Setters and Properties - Learn Object-Oriented Programming in C#
Object-oriented Programming
in C#
15% completed
Search Module
Introduction to Object-
Oriented Programming
in C#
Access Modifiers
Fields
Methods
• An Analogy
• Getters and Setters
• Properties
• Auto-Implemented Properties
An Analogy
Consider the vending machine example. In this machine, there are several
components working together at a micro level to make the vending machine
functional at a macro level. There may be a display screen whose purpose is to
provide an interface to the users. There may be a productGot
dispenser
any feedback?whose purpose
Get in touch with us.
What we want to emphasize here is that there are many components providing
their specific functionalities that don’t know each other’s internal implementation
or working details. The screen doesn’t need to know how the cash collector
component is internally keeping track of the total money collected until now, nor
does the cash collector need to know how the screen is controlling its brightness.
While designing classes in OOP we follow a similar technique. Our classes should
hide their internal implementation details and logic from the other classes. This is a
very important concept called Encapsulation and we are going to explore it in detail
in the upcoming chapter.
We already know that we declare fields in our classes. These fields actually
represent the implementation details of our classes. In order to hide these from
other classes, we need to declare them as private. The question here is “If all the
fields should be private then how can we access and use these in our code in Main() or
some other class?”
These two types of methods are very popular in OOP. A get method retrieves the
value of a particular field, whereas a set method changes its value.
Get and set methods are implemented for fields that are declared private. It
is considered a good practice to declare all the fields private.
1 // VendingMachine class
2 class VendingMachine {
3
4 private int _count; // member field count
5 private int _capacity = 100;
6
7 // Setter method to set the count of the products
8 public void SetCount(int x) {
9 if(x >=0 && x <= _capacity) { // count should always be positive and less than or equ
10 _count = x;
11 }
12 }
13
14 // Getter method to get the count of the products
15 public int GetCount() {
16 return _count;
17 }
18
19 }
20
21 class Demo {
22
23 public static void Main(string[] args) {
24 var vendingMachine = new VendingMachine();
25 vendingMachine.SetCount(88); // calling the setter method
26 Console.WriteLine("The count is: {0}", vendingMachine.GetCount()); // calling the ge
27 }
28
Got any feedback? Get in touch with us.
29 }
We defined the getter and setter methods for the private field count in the above
code. At line 8 we notice that certain conditional statements can also be checked
while setting or accessing the value using setters and getters. Allowing arbitrary
public access to a field can result in all sorts of runtime errors. Setters enable
controlled access to private fields.
Properties
Note the declaring fields as private and then implementing the getter and setter
methods is a common case. C# provides an alternative syntax called “Properties”,
which makes it easier to implement controlled public access to private state.
// VendingMachine class
class VendingMachine {
class Demo {
We can see how easily we can get and set the values of a private field by
Got any feedback? Get in touch with us.
implementing a property. While setting the value of the field we have used the
keyword value.
vice versa.
Just like getter and setter methods we can also use conditional statements while
implementing a property. Another thing to note here is that while accessing a
property in Main() we have used the dot, ., operator.
Auto-Implemented Properties
What if someone tells us that we can shorten the code of properties to achieve
similar functionality to that of getter and setter methods? That would be awesome,
wouldn’t it?
class VendingMachine {
}
Run Save Reset
In the above code, we can notice how efficiently, on line 5, we have done everything
using the auto-implemented property. This property is called auto-implemented
because when we use it, the compiler itself declares a private field and implements
its getter and setter for us.
This ends our lesson about the getters, setters, and properties. In the next lesson, we
will learn how to create objects in a more efficient and usable way.