Since I learned about auto properties, I try to use them everywhere. Before there will always be a separate member for each property that I had, that I will use inside the class. Now this is replaced by the auto property. I use a property inside my class in ways that usually use a regular member field. The problem is that the property starts with capitol, which makes it look a little weird imho when used that way. I did not mind the fact that the properties begin with the capitol earlier, because they will always be behind the "point". Now I have discovered that the prefix of all the properties that I use internally is with this. to calm my feelings.
My dilemma is that before I was always a bit against the prefix of using all internal elements with this. unless it’s necessary "(for example, in the setter or constructor). Therefore, I’m kind of looking for a second opinion on this. Is there a standard good way to do this? Should I just stop complaining (I have a tendency to be" ant humper "(Dutch expression))?
Before:
class Foo { private Bar bar; public Bar Bar { get { return bar; } } public Foo(Bar bar) { this.bar = bar; } public void DoStuff() { if(bar != null) { bar.DoMethod(); } } }
After:
class Foo { public Bar Bar {get; private set;} public Foo(Bar bar) { this.Bar = bar;
Update
Opinions seem to be changing, although more people prefer the this. prefix this. . Before auto properties, I have always been pretty much against the this. prefix this. instead of constructors and in setters (as I mentioned earlier). But now I just don't know anymore.
Additional note. The fact that it is also common to a property name, the same as a class ( public Bar Bar { get; private set; } ), also makes me inclined to prefix. Every time I type Bar.DoMethod() , it seems to me that it looks like a static method. Although VS will be the color of Bar , if it is a static method, and you cannot have a static and instance method with the same signature. When it is painted, it becomes clear that this is a static method, but when it is not painted, it is not 100% sure that it is not a static method. For example, you can simply omit the using statement, but also because I'm not used to binding a non-paintable one, whether it's a static call or not. Before I immediately see this, capitalize the first letter in the case of a member or “dot” in the case of a property (for example, “dot” after foo in (Foo)foo.Bar.DoMethod() ).
(It’s hard to choose “Accepted Answer” at the moment)
c # this prefix
Matthijs wessels
source share