When it comes to a class what is the difference between declaring a public variable, and a private one plus using an auto implemented property? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

When it comes to a class what is the difference between declaring a public variable, and a private one plus using an auto implemented property?

I mean i don't see the difference, I could get and set the variable value from the main program in both cases, can you help me? I know there must be a reason

25th Aug 2016, 1:27 AM
William Allen
William Allen - avatar
2 Answers
0
You most likely use that when you want to restrict setting and getting for other classes and/or want to do something else too when setting or getting a variable. For example save to the database when setting.
25th Aug 2016, 3:04 PM
LetSPlaYRagE23
LetSPlaYRagE23 - avatar
0
It's about scope of given object. See topic encapsulation. Public access method or variable can be accessed from any point in your application. With private access you restrict access to given class only
25th Aug 2016, 5:57 PM
Peter