Hello there my fellow reader.
I started this blog a few months ago and didn’t post anything… (Shame on me)
But this is about to change. NOW!
Let’s get started.
So, what about Properties and Accessible Data Members? What should I use?
Before answering this question I would like to bring up to this post the concept of Class in OOP (Object-Oriented Programming).
You know that in software development, an Object is a representation of something from the Real World. i.e. Student, Car, Customer, and so on…
That being said, some languages have a lot of similarities in their implementation way, like C# and Java. Everybody from the early 90s in IT is aware of that (maybe younger too). So, there is a pillar in the OOP called Encapsulation, which consists of hiding and restricting the functionality and how our code works.
For example, we have the following class with a name data member:
public class Client
{
public string _name;
}
The client class above is a declaration of what a Client has, a name (Just an oversimplified example). That means our class has a public field name (a property) _name. But that is a bad practice because, with a public field, anybody can access it directly either intentionally or not. And we don’t want that, right?
Exactly, we want a class with a private string _name;
by changing the access modifier and now we don’t have access to it. So how can we manipulate our Client’s name?
We have a couple options:
We could create methods to manipulate the _name property!
Yes, we could, then we will end up with two methods GetName
and SetName
. But, can you imagine if we have 10, 20, or 30 fields in our class? We will have N properties + N*2 methods implemented in our class, and sometimes that could be bit annoying specially if we are not doing anything special with the data that is being set.
That, C# gives us some nice options, let’s see them.
Let’s say you are not doing any special validation in the name property, just basic setting and getting. You could write it like this:
public class Client
{
public string Name { get; set;};
}
That way you don’t need to worry because you will have a Name property with the getter and setter automatically, that’s the reason this approach is called Auto-Property. And you could use it like this.
var client = new Client();
client.Name = "John"; // that's how you set it
var clientName = client.Name; // and that is how you get it
Console.WriteLine(clientName); // It will print "John" on the console
The .NET is smart enough to know what to do with Auto-Properties for you. So you can save some time by typing some lines of code that in the end would return the same thing.
If you need to validate some property while setting its value you can make use of Field Backed Property like this.
public class Date
{
private int _month;
public int Month
{
get => _month; // return the month field
set
{
//check if the new value is less than
//and it is between 1 and 12, thenm assign e it to the field
if ((value > 0) && (value < 13))
{
_month = value;
}
}
}
}
in C# 13 in .NET9 this implementation changed a little bit. Instead of using explicitly the property _month, now you can just use the field
keyword. Basically, it creates a backed field “behind the scenes” with name field, and you will have the same effect, and you don’t need to return the backed field if you do not have to manipulate if of course. You can find more information about the field keyword here.
public class Date
{
public int Month
{
get;
set
{
if ((value > 0) && (value < 13))
{
field = value;
}
}
}
}
This is one example, of data validation, but the possibilities and endless.
The thing is, do not use Accessible Data Members, prefer to use Properties in your implementation, if you check the Client example above, if something changes in your logic, your life can be made easier because you don’t need to check your entire solution duplicating logic. This is also about code quality.
You can learn directly from Microsoft’s documentation here.
Thank you for reading.
As my first official post, it would be very important to me if you could leave a comment below, that would help me a lot also your suggestion is welcome.
I wish you a nice one and see you on the next post.
Gio!