I’ve seen some guides or blogs that say using this to access a class’s own members is bad. However, I’ve also seen some places where professionals are accessing with this. I tend to prefer explicitly using this, since it seems to make it clear that the thing I’m accessing is part of the class.
this.MyProperty = this.GetSomeValue();
Is there some advantage or disadvantage to using this? Is it simply a stylistic preference?
Having gone from using
thisfor years, to finding not many people (atleast in my experience) use it, I eventually changed. The benefits I can see of having this-less code:_myVarfor private variables, which don’t need athisas they’re always member variables.e.g.
I can see that if you don’t use the underscores naming convention, and have a large method with a weighty body it could lead to some confusion.
Static methods or properties can occasionally confuse things, but very rarely.
You will obviously always need the
thiskeyword when passing references, for example:(I write C#, but my answer relates to Java)