Should I use properties in my C # programs, or should I use get / set accessors?

I was reading a book called C # Yellow Book by Rob Miles when I came across this statement:

Programmers love new shiny toys. They are very interested in using the features of the language to show off. Properties may be a bit like this. When considering properties compared to get and set methods, I’m rather a fan of the old-fashioned get and set methods, because you know where you are with them. On the other hand, I see where properties can make life easier if used correctly.

I'm not sure what he means. What are you offering? What does MS offer?

+4
source share
7 answers

I think you should consider some programming tips.

When in Rome do what the Romans do.

.NET has properties rooted in all its libraries, constructors, and code generators. This is almost an integral part of the platform. If you decide to use get / set accessors instead of the usual properties, you will create a library that has a completely different look than what any other programmer expects.

Using get / set accessors will only increase the likelihood that you will create code that is incompatible with various tools. For example, there are many tools that provide special properties and fields for a specific case and provide special functions for them. You will have an equivalent design, but without tool support.

+18
source

I wrote this part of the book as a Java programmer who switched to C # several years ago. I think I will reread the text in the next version and make it more understandable.

If you want to make sure that the user of your class knows that some code will be launched when he uses some thing, then including it in the method makes this explicit. In addition, the use of methods allows returning error conditions without the need to exclude exceptions. There is no effect, since the compiler will still convert properties into methods.

I use properties a lot in the code I write, especially for things like state. I never advocated not using properties, and made sure that you correctly used the right situation.

+8
source

I do not know who Rob Miles is, but if your quote is accurate, then I have already lost respect for him.

Everything in .NET uses properties. If your code does not use properties, then it will be about the only piece of code that does not.

+5
source
myObject.Property++; 

vs

 myObject.SetProperty(myObject.GetProperty() + 1); 

Yes...

The explicit getter / setter methods should be used when nontrivial calculations / processing is performed, therefore, when the actual action is performed.

+2
source

I love properties. In the Design Guide for Class Libraries, they recommend using Properties (especially for a state that is independent of another state).

0
source

The best way to think about this is by matching real-life objects. Say you are a person in real life who needs to be designed. What would you call some of your attributes in terms of hair color, height and width, etc.? As soon as you start thinking about these lines, it will be more useful, since each of them has its own way of thinking.

0
source

If you do not use "Properties", you will lose a very important function that I find extremely useful in .NET: Binding. You can only bind to properties in .NET, so if you use get / set accessors, you are not playing well with others.

0
source

All Articles