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. Why do we have properties?

Why do we have properties?

Scheduled Pinned Locked Moved C#
csharpjavadatabasedotnet
6 Posts 5 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.
  • R Offline
    R Offline
    Russell Jones
    wrote on last edited by
    #1

    From everything I read the compiler doesn't create properties in the IL it creates, it creates a method for setting a value and a method for getting the value. In the java world using accessor methods in the code is the way things are done and it works fine. Apart from the syntax of setting or getting a property value from code, which may be a bit nicer than the method syntax and the fact that people should learn to behave themselves in get{} code (I once worked with someone who changed the selected index of a combo in a VB6 Property Get for a comboBox, great fun when putting watches on things) I can see no advantages in using properties over accessor methods. At the same time I can see problems like the inability of c# to create Property delegates (i'm talking well behaved ways here) encouraging me to use accessor methods. Can anyone think of reason why I should use properties in my code? Do we just blindly do things this way because we always used them in VB or because the books teach it that way? Russ

    M J M V 4 Replies Last reply
    0
    • R Russell Jones

      From everything I read the compiler doesn't create properties in the IL it creates, it creates a method for setting a value and a method for getting the value. In the java world using accessor methods in the code is the way things are done and it works fine. Apart from the syntax of setting or getting a property value from code, which may be a bit nicer than the method syntax and the fact that people should learn to behave themselves in get{} code (I once worked with someone who changed the selected index of a combo in a VB6 Property Get for a comboBox, great fun when putting watches on things) I can see no advantages in using properties over accessor methods. At the same time I can see problems like the inability of c# to create Property delegates (i'm talking well behaved ways here) encouraging me to use accessor methods. Can anyone think of reason why I should use properties in my code? Do we just blindly do things this way because we always used them in VB or because the books teach it that way? Russ

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, One reason could be the user support over the designer (VisualStudio Designer).

      arachnoid wrote:

      Do we just blindly do things this way because we always used them in VB or because the books teach it that way?

      Never worked with VB before, but I'm also a fan of properties. Martin

      R 1 Reply Last reply
      0
      • R Russell Jones

        From everything I read the compiler doesn't create properties in the IL it creates, it creates a method for setting a value and a method for getting the value. In the java world using accessor methods in the code is the way things are done and it works fine. Apart from the syntax of setting or getting a property value from code, which may be a bit nicer than the method syntax and the fact that people should learn to behave themselves in get{} code (I once worked with someone who changed the selected index of a combo in a VB6 Property Get for a comboBox, great fun when putting watches on things) I can see no advantages in using properties over accessor methods. At the same time I can see problems like the inability of c# to create Property delegates (i'm talking well behaved ways here) encouraging me to use accessor methods. Can anyone think of reason why I should use properties in my code? Do we just blindly do things this way because we always used them in VB or because the books teach it that way? Russ

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        Its just eye candy - as you rightly state, under the hood the IL is a get_PropertyName() and set_PropertyName(value) method. I use properties as I prefer the look of int myValue = myObject.MyProperty than int myValue = myObject.GetMyProperty();

        --- How to get answers to your questions[^]

        1 Reply Last reply
        0
        • R Russell Jones

          From everything I read the compiler doesn't create properties in the IL it creates, it creates a method for setting a value and a method for getting the value. In the java world using accessor methods in the code is the way things are done and it works fine. Apart from the syntax of setting or getting a property value from code, which may be a bit nicer than the method syntax and the fact that people should learn to behave themselves in get{} code (I once worked with someone who changed the selected index of a combo in a VB6 Property Get for a comboBox, great fun when putting watches on things) I can see no advantages in using properties over accessor methods. At the same time I can see problems like the inability of c# to create Property delegates (i'm talking well behaved ways here) encouraging me to use accessor methods. Can anyone think of reason why I should use properties in my code? Do we just blindly do things this way because we always used them in VB or because the books teach it that way? Russ

          M Offline
          M Offline
          Martin23
          wrote on last edited by
          #4

          You don't have to use properties if you prefer not to. Virtually everybody does though, as they make code much easier to understand.

          1 Reply Last reply
          0
          • M Martin 0

            Hello, One reason could be the user support over the designer (VisualStudio Designer).

            arachnoid wrote:

            Do we just blindly do things this way because we always used them in VB or because the books teach it that way?

            Never worked with VB before, but I'm also a fan of properties. Martin

            R Offline
            R Offline
            Russell Jones
            wrote on last edited by
            #5

            I hadn't thought about that. I guess things like databinding use properties aswell. Maybe if the compiler allowed you to use either Propertyname = value or set_Propertyname(value). That way the delegate problem would be solved without relying on c#'s underlying implementation and we would have the best of both worlds. Russ

            1 Reply Last reply
            0
            • R Russell Jones

              From everything I read the compiler doesn't create properties in the IL it creates, it creates a method for setting a value and a method for getting the value. In the java world using accessor methods in the code is the way things are done and it works fine. Apart from the syntax of setting or getting a property value from code, which may be a bit nicer than the method syntax and the fact that people should learn to behave themselves in get{} code (I once worked with someone who changed the selected index of a combo in a VB6 Property Get for a comboBox, great fun when putting watches on things) I can see no advantages in using properties over accessor methods. At the same time I can see problems like the inability of c# to create Property delegates (i'm talking well behaved ways here) encouraging me to use accessor methods. Can anyone think of reason why I should use properties in my code? Do we just blindly do things this way because we always used them in VB or because the books teach it that way? Russ

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              One small adavantage is you can do something like this: MyStringProperty += " Concatenate this..."; for getters and setters this would be SetStringProperty(GetStringProperty() + " Concatenate this..."); Same for other type (int, double,...)


              V. Stop smoking so you can: enjoy longer the money you save.

              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