Click here to Skip to main content
15,938,446 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When using the this keyword to reference methods/variables in the current class instance does it make any difference at all if I use it or not? (I mean where I could use it, not where I should or have to use it)

I tend to declare my internal/privates with a leading '_', so I don't need this as an identifier. But was just wondering if it made any bit of difference to the complier etc.

If it doesn't make a difference, what's the best practice on using it?

The only difference it makes, is that it gives you some intellisense, and explicitly scopes what would otherwise be implicit. The compiler will ignore it.
Share this answer
cjb110 27-Aug-10 17:07pm    
thanks, exactly the type of response I was looking for!
Hi cjb110,

That's a very good question. I don't feel it as useful every time calling "this". You should use the keyword "this" only when it is require.

Suppose, in the following case:

public class Person
    private string fullName;
    public Person(string fullName)
        this.fullName = fullName;

Though it is not a very good idea to use the same name but if you want to keep consistency, in such case you can use "this". In other case, it is not require at all.

If you use "ReSharper", there it doesn't recommend you to use the "this" keyword in all places. Just think about it and ask you "Why should I use something which is not useful?"
Share this answer
The linker uses the narrowest scope when resolving names. The this keyword (and in VB, the Me keyword) tells the linker to use the class-level object, even if there is a local object with the same name. this is also used for declaring extension methods in C#, but that's pretty arcane and not something most people will want to do.

As long as you do not duplicate class-level object names inside of methods or local code blocks, this is usually just documentation.
Share this answer
Christian Graus 27-Aug-10 17:15pm    
Ah - yes. I was trying to think about how it could be possible for this to be needed, I forgot about local objects that hide the object that exists at class level. This is always a hideous thing to do, as you say, but it DOES make 'this' necessary.
DaveyM69 27-Aug-10 18:32pm    
To Christian: "hideous thing to do"
I have to own up and say I normally give my fields and contructor parameters exactly the same name and use this to assign the parameters to the fields... saves all that horrible _field or m_Field stuff!
Chris Trelawny-Ross 27-Aug-10 19:26pm    
I guess I could call extension methods arcane - but they're totally awesome and as more people get to understand what they offer I suspect that they'll become commonplace.

I find that I'm using them more and more when they improve readability - such as

... if (someString.DoesNotContain("some search text")) ...

which I find to be clearer and safer than the alternative:

... if (!someString.Contains("some search text")) ...

which can be inverted by one accidental character deletion. I consider that the '!' character nestled inside an extension method to be safer because I'm far less likely to be editing the extension method after it's written than I am to be editing the code that uses/would use it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900