Stylecop tells me to add this keyword, but it's redundant - any performance implications?

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

Does redundant code mean that it is not needed (obviously), and the compiler will do nothing with this keyword? Therefore, I assume that this keyword is just for clarity.

Also, with the CLR, do things like this fall, sequentially in different languages? Therefore, if the answer is that the compiler does not even affect the this keyword , and this is just for presentation and clarity, then is this true for VB.NET? I assume this is all for clarity, as stylecop follows this, and Fxcop (which I will use later) monitors the quality of my code from a technical point of view.

thanks

+5
source share
6 answers

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

The IL that he compiles will not be different.

+12
source

, .

using System;

class Foo
{
    String bar;

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

this, bar bar. , .

+3

/ this - , ldarg.0 IL.

this ( , ctor -chaining this ): . , this ( ).

, ...

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

# ( VB.NET). , . , #:

public class MyClass
{
    int rate;

    private void testMethod()
    {
        int x;

        x = this.rate;
    }
}

, , , ( SO, ). VB , .

0

, "this" .

0

, , . Python "self".

, CLR, , , ? , , , , VB.NET?

JVM ( CLR, ) "this" , - , , , , .NET , .

Then it depends on the language. For example, JScript (and even JScript.NET) does not allow to omit “this”, for example Python, because there are functions (therefore, “this.a ()” is a method call, “a ()” is a function call), and because the compiler does not know the members of any types - they are known only at runtime (well, this is not an impossible problem to solve really, another problem is more relevant).

0
source

All Articles