Any example situation where I should use get and set in C#? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Any example situation where I should use get and set in C#?

Hello everyone, I've come across this in a variety of video tutorials and I understand what it does, but why should I use get and set like this, while I could also get the value of "number" easier, as an example? public int Number { get { return number; } } Thanks for taking your time reading and maybe leaving an answer!

14th Feb 2018, 7:49 AM
DelightedCat
1 Antwort
+ 2
Other parts of the program can 'peek' in and take an attribute. Set-get-ers are in place to prevent leakage of information and to block access to parts of the program (where frankly, it doesn't need this or so variable). If your code is dissected, it can be easily recognised whether someone else has tampered with it. Because it differed to how you last saw it...
14th Feb 2018, 11:58 AM
ghostwalker13
ghostwalker13 - avatar