Solved

Quick, Easy C# Question - Use of this Keyword

Posted on 2007-03-29
5
172 Views
Last Modified: 2010-04-16
This may seem like a silly question but I can't remember what the difference is (if there is any) between calling a method this way: this.MethodName();

and this way MethodName();

In each case, the method is local.

As far as I know, these are equivalent.
0
Comment
Question by:marmaxx
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 18818600
No difference -- except that this.MethodName() must be public.  MethodName() can be private.
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 50 total points
ID: 18818895
this.MethodName() can be used to access private methods as well...

Only difference is this.MethodName() cannot be used to access static methods.
0
 
LVL 3

Expert Comment

by:Nico
ID: 18819822
So, basically "this" refers to the particular -instance- of the class that the method is in. (so in case of a static method there would nto be an instance hence there would be no "this" .. )
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 18819847
>>Only difference is this.MethodName() cannot be used to access static methods.

Or is it that "this" cannot be used WITHIN a static method?
0
 
LVL 25

Assisted Solution

by:dstanley9
dstanley9 earned 50 total points
ID: 18819870
Both are true.  
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question