Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foo() vs this.Foo() [closed]

Tags:

c#

refactoring

I have a co-worker who uses a C# refactoring tool. The tool for some reason perfers:

this.Foo()

over

Foo()

Now we've asked him to turn it off simply because it's annoying to have all the code re-written automatically, but that's not the point.

Am I missing something, or is this just wrong? Why on earth would I want this.Foo()?

like image 588
Timothy Baldridge Avatar asked Apr 14 '11 19:04

Timothy Baldridge


1 Answers

As other answers here point out, this is largely a matter of style.

Many times a call to Foo() would be unambiguous without the this., but there are times where it might add clarity. For example:

EventHandler foo = GetEventHandlerFoo();
EventHandler baz = GetEventHandlerBar();
foo();
this.Bar();
baz();

In this case, the this. prefix helps to make the method invocation stand out from the delegate invocations.

Is this necessary? No, and Lasse V. Karlsen correctly points out that the this. is just a surrogate for a naming convention that makes it unambiguous.

But I find that I will frequently use the this.Foo prefix on local properties for a similar reason, where again it is more a matter of style.

like image 80
Daniel Pryden Avatar answered Oct 10 '22 06:10

Daniel Pryden