Public vs Private Memory Question
-
Hi All, My teacher stated - "Using public variables in a class will allocate memory for the lifetime of an application. So we should use private variables and use Get/Set." I believe that this is false and the get/set actually add some memory. Forgetting what is better programming practice, is he right about the memory? Signed, Puzzled in C#
-
Hi All, My teacher stated - "Using public variables in a class will allocate memory for the lifetime of an application. So we should use private variables and use Get/Set." I believe that this is false and the get/set actually add some memory. Forgetting what is better programming practice, is he right about the memory? Signed, Puzzled in C#
No. What he said would apply to static variables, those exist and cost memory from the first time the class is touched to the end of the app's execution. public/protected/private doesn't change the cost. Adding properties is adding code, hence costs memory and disk space, however it is a good practice. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
No. What he said would apply to static variables, those exist and cost memory from the first time the class is touched to the end of the app's execution. public/protected/private doesn't change the cost. Adding properties is adding code, hence costs memory and disk space, however it is a good practice. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Does adding a property with a simple Get/Set to a private variable actually add to the cost of the runtime? I remember hearing in Delphi that the compiler had the smarts to compile a simple property exactly as if the private field had been accessed publicly - i.e. without overhead. Sure it takes a few bytes of source code, but I wonder if there really is any cost to the executable?
___________________________________________ .\\axxx (That's an 'M')
-
Does adding a property with a simple Get/Set to a private variable actually add to the cost of the runtime? I remember hearing in Delphi that the compiler had the smarts to compile a simple property exactly as if the private field had been accessed publicly - i.e. without overhead. Sure it takes a few bytes of source code, but I wonder if there really is any cost to the executable?
___________________________________________ .\\axxx (That's an 'M')
at the IL level a property "xyz" gets compiled as a get_xyz() or set_xyz() method. the JIT compiler might inline that, I don't know if it does so. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
at the IL level a property "xyz" gets compiled as a get_xyz() or set_xyz() method. the JIT compiler might inline that, I don't know if it does so. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
there are lots of things a compiler should do. In my experience comparing actual behavior with a published list of features and qualities tends to be very disappointing. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
there are lots of things a compiler should do. In my experience comparing actual behavior with a published list of features and qualities tends to be very disappointing. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages