+ 5
Purpose of private modifier
Whats the point of having a private int when it is changeable by another public int. i mean isn't this the purpose of a private modifier?... to remain unchanged...
7 ответов
+ 12
The purpose of making a variable private isn't to make it unchangeable. It is to make sure that the variable itself can only be modified by, and through that specific class.
+ 12
That's basically the purpose of setters. Although other classes may be able to change the private variable of another class via the public setter, it means that the only means of modifying the private variable is through that specific setter. This may not seem to be a big deal, but when you are working on a huge project, this saves the programmer a lot of work when it comes to debugging, because it is easy to track which block of code is throwing the error.
+ 8
What Hatsy Reid said + in a setter you can also validate the argument, throw an error if it's invalid and set it only if it is valid.
+ 2
got it. thanks for the information <3
+ 2
yea I just realised it in the next lesson. tnx
+ 1
even when [public string Name(get; set;)] is called from another class? I mean other classes have access to a public code and they can change the private int trough this code.
+ 1
In short, public property is "door" with filter to capsulated code. Like a gates with guardians in a castle walls. Imo.