+ 1
Explain get and set in C#.
If we want to change something in private, Why we make it private?
2 Respostas
+ 6
When something is made public, it can be so easily accessed or even modified, no restriction or validation can be implemented.
When we create a setter property for a private member, we are allowing a controlled means of modification. That is, we can implement restriction on the new value, define some default when the new value passed doesn't qualify etc.
It's part of encapsulation, where an encapsulated member (a private one in this case) can be modified, but the modification is moderated.
+ 2
The get method returns value of the variable name.
The set method assigns a value to the name variable.