Properties vs Get Set Accessor methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Sprout

    Properties vs Get Set Accessor methods

    Why is it considerd best practice to use Properties rather than Get and Set
    accessor methods?


    -Tim Sprout


  • Jon Skeet [C# MVP]

    #2
    Re: Properties vs Get Set Accessor methods

    Tim Sprout <tman@ptialaska .netwrote:
    Why is it considerd best practice to use Properties rather than Get and Set
    accessor methods?
    Because they're more readable, and because tools which are designed to
    use properties will find properties but not accessor methods. Accessor
    methods are basically just following a convention - properties also
    follow a convention, but have extra metadata declaring them to be
    properties.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Larry Smith

      #3
      Re: Properties vs Get Set Accessor methods

      Why is it considerd best practice to use Properties rather than Get and
      Set
      accessor methods?
      Because it's a high-level, built-in feature designed explicitly for this
      purpose and therefore much cleaner IMO. Not only do they instantly convey
      their intended purpose to other readers (improving your code's legibility),
      but they're also specifically recognized by different tools, controls, etc.
      For instance, you can pass an object to the native "PropertyGr id" control
      and it will automatically populate the control with all properties in your
      object.


      Comment

      • Bruce Wood

        #4
        Re: Properties vs Get Set Accessor methods

        On Feb 1, 8:34 am, "Tim Sprout" <t...@ptialaska .netwrote:
        Why is it considerd best practice to use Properties rather than Get and Set
        accessor methods?
        >
        -Tim Sprout
        In colloquial terms, someone hands you a screwdriver. Do you say, "Why
        should I use this, when a butter knife can get the job done?"

        Properties formalize an informal standard. In essence, the compiler
        now supports and enforces rules about something that (in Java, for
        example) used to be the programmer's responsibility to get right. I
        can't see any downside to that, although I do remember one poster here
        who vehemently maintained that properties were crap and that getter
        and setter methods were the only way to go. I can't recall why, sorry.

        Comment

        • Dan Reber

          #5
          Re: Properties vs Get Set Accessor methods

          This may be a stupid question but am I using (see below) Properties or
          Accessor methods? If am using Accessor methods, how do I convert the below
          to Properties?

          Thanks

          Dan


          public int ObjectID
          {
          get
          {
          return objectID;
          }
          set
          {
          objectID = value;
          }
          }


          "Tim Sprout" <tman@ptialaska .netwrote in message
          news:12s45hk25m q5gdb@corp.supe rnews.com...
          Why is it considerd best practice to use Properties rather than Get and
          Set
          accessor methods?
          >
          >
          -Tim Sprout
          >
          >

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Properties vs Get Set Accessor methods

            Dan Reber <dreber@nospam. comwrote:
            This may be a stupid question but am I using (see below) Properties or
            Accessor methods? If am using Accessor methods, how do I convert the below
            to Properties?
            That's a property. It generates accessor methods behind the scenes, but
            it's a property. If you wrote "plain" accessor methods, it would be
            something like:

            public int GetObjectID()
            {
            return objectID;
            }

            public void SetObjectID(int value)
            {
            objectID = value;
            }

            The generated accessors have an extra _ in the name, as well as more
            metadata.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Dan Reber

              #7
              Re: Properties vs Get Set Accessor methods

              Great, thanks.

              Dan

              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:MPG.202c44 b712090add98d81 1@msnews.micros oft.com...
              Dan Reber <dreber@nospam. comwrote:
              >This may be a stupid question but am I using (see below) Properties or
              >Accessor methods? If am using Accessor methods, how do I convert the
              >below
              >to Properties?
              >
              That's a property. It generates accessor methods behind the scenes, but
              it's a property. If you wrote "plain" accessor methods, it would be
              something like:
              >
              public int GetObjectID()
              {
              return objectID;
              }
              >
              public void SetObjectID(int value)
              {
              objectID = value;
              }
              >
              The generated accessors have an extra _ in the name, as well as more
              metadata.
              >
              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              • Robson Siqueira

                #8
                Re: Properties vs Get Set Accessor methods

                IMHO, I would - and this is what I do most of the time - is to use
                properties. Why? I am a crazy "dynamic business rules" kind of person. When
                using reflection, for instance, properties are your friend.

                Also, properties are more readable than accessor methods, IMO.

                Again, it is a personal opinion.

                --
                Regards,
                Robson Siqueira
                Enterprise Architect
                "Tim Sprout" <tman@ptialaska .netwrote in message
                news:12s45hk25m q5gdb@corp.supe rnews.com...
                Why is it considerd best practice to use Properties rather than Get and
                Set
                accessor methods?
                >
                >
                -Tim Sprout
                >
                >

                Comment

                • Tim Sprout

                  #9
                  Re: Properties vs Get Set Accessor methods

                  Why is it considerd best practice to use Properties rather than Get and
                  Set
                  accessor methods?
                  >
                  >
                  -Tim Sprout
                  Great responses. Thanks.

                  -Tim Sprout



                  Comment

                  Working...