Is there a penalty using 'this' to call class methods?

  • Thread starter Thread starter Robert Linder
  • Start date Start date
R

Robert Linder

Is there a penalty using 'this' to call class methods?

I like using 'this' because it shows me what is in my current class.

Does it matter to .NET that I use 'this.myMethod()' instead of 'myMethod'?
 
Every method you call on an instance in C# uses this. If you don't type it
explicitly, it's inferred (it compiles to the same code).
-mike
MVP
 
it is my understanding that there is no penalty whatsover to using this , it
is not recommended for style reasons, but perfomance should not be effected.
 
"style reasons"? That'd be a rather personal preference. Sometimes
explicitly specifying this can increase readability. Other times, it's
required, say if you have a private field and in a method you have an
argument with the same name, and you want to assign the parameter to the
field (constructors...).

-mike
MVP
 
yes.. well in your latter example the use of "this" is more of a necessity.
The style reasons are obvious in the sense that it does not aid readabilty
when "this" is used to reference every single member as per the origional
post (especially in a large class). Your point about personal preference is
very true *but* in cases where code is worked on by multiple people I'm of
the school of thought to try and work towards certain conventions.

Just to finish I will add that the point that I made that it was not
recommended to use "this" to reference every element, was taken from a
book -I just so happened to agree with it, however I will add that I am by
no means an authority in C# (yet -but trying!) so the opinions I give are
purely from a personal point of view.
 
Back
Top