Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Public vs Private Memory Question

Public vs Private Memory Question

Scheduled Pinned Locked Moved C#
questioncsharpvisual-studioperformance
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thenutz72
    wrote on last edited by
    #1

    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#

    L 1 Reply Last reply
    0
    • T thenutz72

      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#

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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


      L 1 Reply Last reply
      0
      • L Luc Pattyn

        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


        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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')

        L 1 Reply Last reply
        0
        • L Lost User

          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')

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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


          L 1 Reply Last reply
          0
          • L Luc Pattyn

            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


            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            It should, since the body of the method will be smaller than 16 bytes

            L 1 Reply Last reply
            0
            • L Lost User

              It should, since the body of the method will be smaller than 16 bytes

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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


              L 1 Reply Last reply
              0
              • L Luc Pattyn

                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


                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                True :(

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups