This recently hit me on a project I was working on. Most people are familiar with property recursion:
public int Test { get { return this.test; } set { this.Test = value; } } private int test;
You accidentally put an uppercase T in this setter, and you opened yourself up to a StackoverflowException . Worse, if you did not identify it, often the visual studio will automatically fix the case for you in an unacceptable condition.
I did something similar, but in the constructor recently:
public TestClass(int test) { this.Test = Test; }
Unfortunately, here you are not getting a StackOverflowException, now you have a programming error. In my case, this value was passed to the WebService, which instead used the default value (which was not 0), which made me skip the fact that I assigned it incorrectly. All integration tests passed because this service did not say
"Hey, you forgot this really important field!"
What steps can I take to avoid this behavior? I have always been advised not to define variables like the following, and I personally don’t like them, but I can’t come up with any other options:
private int _test; private int mTest;
EDIT
The reasons that the underscore or the prefix m is undesirable, as a rule, I can think of:
- readability
- It's a little harder to scroll through elements if you inherit from third-party classes when you get a combination of styles.
c # properties recursion
Ian Oct 30 '13 at 13:02 2013-10-30 13:02
source share