An example of Auto-implemented properties in C# is for example:
public double TotalPurchases { get; set; }
I understand what they are, and as they say the "compiler creates a private field that can only be accessed through get and set.
My question is what is the point of this?
I mean in other languages, the point was: You have a variable that you make private and then you declare some functions public through which you can access that variable- NEVER directly. Why? Well because then you can implement that "variable" the way you want it and the user of your class is oblivious to that, he just gets and sets what he sees is a property of the class.
But here, although you have a private variable that can be accessed only through get and set (same as above) you also have a very public variable that actually you access directly, defeating the purpose of get and set in the first place.
I mean if you are going to do
cust1.TotalPurchases += 499.99;
why not just have
public double TotalPurchases;
and be done with it??
EDIT: This question has been marked as a "possible duplicate" which in my opinion is not, and i will try to explain -in my rather limited english- why it is not.
The author of the "possible duplicate" originally wrote a way that broke totally encapsulation. Later he confessed that he "couldn't bear writing all that " so he just used public fields. After all he didn't care about encapsulation in the first place.
My post is the total opposite. I want to keep encapsulation, since as I previously explained this gives me the liberty to implement TotalPurchases as I see fit. I may use a variable, but I might get that from a DB, or calculate it through a Neural network. The thing is I don't want the user to be oblivious to this.
But, if I have to use get and set as explained I am forced to declare a public variable right? or if I do this
private double TotalPurchases {get; set;}
are these get and set public? I suspect they are not.
So my question is what is the point of breaking encapsulation?