What safety advantage does a property without any logic in set and get have over just making a variable public? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What safety advantage does a property without any logic in set and get have over just making a variable public?

Does it somehow stop a hacker from hex editing the variable directly in the RAM or something else? Or is a property without any get and set logic as unsafe as just making a variable public without properties? If there is no safety without logic for get and set, do programmers then still do it almost always because it doesn't hurt and give the option to add logic later if needed without doing major rewrites?

14th Jun 2019, 2:58 PM
Entrance
2 Answers
+ 4
The get/set properties are probably only good if they were written with some logic to implement data validity I suppose. Although as I understand it, the validity enforcement is only logical, that is, the properties are there to prevent attempts to set, or return invalid data. An advanced coder with good knowledge of how the program manages memory will probably still be able to play around with the program's memory given a suitable tool and enough time; even with the properties being equipped with good logic implementation, because they'd be bypassing the regular ways in doing it. I could be wrong still, as this is just how I see it, so, always cmiiw : )
14th Jun 2019, 5:26 PM
Ipang
+ 4
When you use it, there seems to be no difference. Everything can be put into this field and you do not check anything. This is the same for auto-implemented properties and for fields. However you never know what the future is go a give you. At the first moment a field can look to be sufficient enough. But the next moment you might want to do something that requires a property with a get and set method. If you choose a field in the first moment. You have a problem with software that is interacing with this module. If you choose a auto-implemented property in the first moment. You are fine. https://stackoverflow.com/questions/6001917/what-are-automatic-properties-in-c-sharp-and-what-is-their-purpose Quote As a test, I created a library project and declared a property called TestData. I created a whole new project just to consume this library. All worked as expected. I then changed the property to a public field (the name stayed the same) and copied over the new library DLL without recompiling the consuming project. The outcome was an exception thrown as the code was expecting to find the methods property methods get_TestData and set_TestData, but fields are not accessed via methods. It is something of advanced programming, A senior developer did learn this to me when I was just a beginner. Always use auto-implemented properties!! Because there is not a lot of difference between the two, choose the most advance one. Yes: Sorry first answered your question and then read you post. This is exactly why they are used. Your wrote: it doesn't hurt and give the option to add logic later if needed without doing major rewrites?
14th Jun 2019, 7:37 PM
sneeze
sneeze - avatar