Is always prefixing (auto) properties with a keyword considered good practice?

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; // or Bar = bar; } public void DoStuff() { if(this.Bar != null) { this.Bar.DoMethod(); } // or if(Bar != null) { Bar.DoMethod(); } } } 

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)

+7
c # this prefix
source share
4 answers

Yes, there is a “standard way to do this”: an uppercase letter and this prefix are considered good coding practice. If you use any code checking tool for coding instructions, such as ReSharper or Microsoft StyleCop , it will warn you if you do not use this link, or if you do not run your properties with capital.

Your properties are publicly available. Any public ownership, field or method should begin with capital.

Any property, field, or method that you call inside your own class that is part of this class must be prefixed with this link for readability.

Update : of course, opinions are changing. I like to click this. and then after the dot see only members, instead of seeing all the keywords, just by pressing ctrl-space without any prefix. It helps me. But, at the end ( quote from here ):

Regardless of your opinion, the important thing is that all people in cooperation with the project have the same formatting standards, regardless of what these standards are.

Other links:
Microsoft uses a capital letter for almost any name and property.
More recommendations here .

+6
source share

I highly recommend using ' this. 'where possible. The wireframe design guide recommends this practice. It allows you to know the scope from the point of view of readability and helps to avoid stupid errors, the compiler of which can report at compile time.

+6
source share

And I highly recommend never to use it , as it only reduces clarity. If you actually find yourself in the case when you need it to avoid a collision, I would recommend renaming one of the fields / properties / variables.

The only place I find it acceptable is if it is part of a publicly exposed API where renaming will cause a breaking change.

+2
source share

In the first example, the bar parameter lexically obscures the bar field from the instance. Therefore, you should use this to disambiguate.

In the second example, you do not have such ambiguity and therefore there is no need for a value (i.e. this ). However, you can still prefix if this is your cup of tea. :)

+1
source share

All Articles