tags:

views:

320

answers:

6

I use Stylecop for Resharper and whenever I call something in my class, Stylecop tells me to use This. But the IDE says this is redundant code (which it sure is), so why should I use this?

Does redundant code mean its not needed (obviously) and the compiler won't even do anything with the this keyword? So I assume the this keyword is just for clarity.

Also, with the CLR, do things like this fall consistently across languages? So if the answer is that the compiler doesn't even touch the this keyword and it is just for presentation and clarity, then the same is true for VB.NET? I assume it is all for clarity as stylecop keeps an eye on this and Fxcop (which I will use later on) keeps an eye on my code's quality from a technical point of view.

Thanks

+11  A: 

It's for clarity and to prevent any ambiguity between a class member and a local variable or parameter with the same name.

The IL it compiles to will not be any different.

Andrew Kennan
+3  A: 

Most of the time is just for clarity but some times it is required.

using System;

class Foo
{
    String bar;

    public Foo(String bar)
    {
     this.bar = bar;
    }
}

Here you will need this as it serves to disambiguate between the field bar and the constructor parameter bar. Obviously changing the name of the parameter or field could accomplish the same thing.

Andrew Hare
A: 

In C# this is a reference to the current instance of the class (it's me in VB.NET). It's used generally to fully qualify a class member. For example, consider this C# class:

public class MyClass
{
    int rate;

    private void testMethod()
    {
        int x;

        x = this.rate;
    }
}

this isn't required in the code above, but adds instant clarity when reading the code that rate belongs to the class rather than the method (search SO, you'll find lots of opinions about the use of this). It's semantic behavior is the same in VB--and its use doesn't impose a performance penalty.

rp
A: 

Apart from the clarity examples provided the only other valid usage of the "this" keyword is to pass the current instance of an object as a paremeter.

lomaxx
If I exclude ctor-chaining, there is still another - see my reply.
Marc Gravell
ahh this is true...
lomaxx
A: 

It is just for clarity, and one can argue about what is better. Python doesn't support omitting the "self" identifier at all.

Also, with the CLR, do things like this fall consistently across languages? So if the answer is that the compiler doesn't even touch the this keyword and it is just for presentation and clarity, then the same is true for VB.NET?

In JVM for sure (and also for CLR, I'm almost sure) the code for the "this" keyword is always generated, even if that is omitted from the source - so it's like if the this keyword is always added. So, I don't think that any .NET compiler could generate different output, so there can't be a performance penalty.

Then, it depends on the language. For instance JScript (and even JScript.NET) does not allow to omit "this", like Python, because there are functions (so "this.a()" is a method invocation, "a()" is a function invocation), and because the compiler does not know the members of any types - they're only known at runtime (well, this is not an impossible problem to solve indeed, the other issue is more relevant).

Blaisorblade
+2  A: 

In all cases, there is no performance difference with/without the this - the compiler still does it implicitly, injecting a ldarg.0 into the IL.

Just for completeness, there is one other mandatory use of this (excluding disambiguation, ctor-chaining, and passing this to other methods): extension methods. To call an extension method on the current instance, you must qualify with this (even though for a regular method it would be implicit).

Of course, in most cases, you would simply add a regular instance method to the class or a base-class...

class Foo {
    void Test() {
        this.Bar(); // fine
        Bar(); // compiler error
    }
}
static class FooExt {
    public static void Bar(this Foo foo) { }
}
Marc Gravell