Why do we have properties?
-
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
-
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
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
-
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
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
thanint myValue = myObject.GetMyProperty();
-
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
-
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
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
-
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
One small adavantage is you can do something like this:
MyStringProperty += " Concatenate this...";
for getters and setters this would beSetStringProperty(GetStringProperty() + " Concatenate this...");
Same for other type (int, double,...)
V. Stop smoking so you can: enjoy longer the money you save.