+ 1

Explain get and set in C#.

If we want to change something in private, Why we make it private?

30th Nov 2022, 1:45 PM
Aayush Jat
Aayush Jat - avatar
2 Answers
+ 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.
30th Nov 2022, 3:03 PM
+ 2
The get method returns value of the variable name. The set method assigns a value to the name variable.
30th Nov 2022, 2:03 PM
Anonymous - avatar