leora
leora

Reputation: 196669

Does this. slow down code?

We were having the (never ending) underscore prefix versus no underscore prefix debate on member variables and someone mentioned that is you use "this." instead of "-", your code will be slower due to the "." in "this.". Is this true and can anyone quantify this?

Upvotes: 2

Views: 438

Answers (5)

Marcom
Marcom

Reputation: 4741

Variables represent locations in memory. When compiled a 100character variable and a one letter variable are both converted into numbers. In the same way special characters are translated and wont make any difference on the speed.

Upvotes: 2

dr. evil
dr. evil

Reputation: 27275

Who needs underscores when you got camelCasing? Also I got a say that what you doing sounds like a crazy idea.

Upvotes: 0

Matt Sherman
Matt Sherman

Reputation: 8358

Seems to me that "this." is a disambiguator at compile time. It tells the compiler the scope of the variable. It may be unnecessary, since the compiler will need to figure out scope in any case. But I can't imagine there is any performance downside, perhaps even a microscopic upside as you are "hinting".

Once the code is compiled (ie, at runtime), I imagine "this." is utterly irrelevant.

So it's a style choice. Some people prefer terseness. I like "this." because it adds clarity, when used correctly. It tells other developers where a function or property lives. I use it for any public method or property. I don't usually use it with private members.

Juval Lowy has a very nice C# style guide here: http://www.idesign.net/

Upvotes: 5

bendewey
bendewey

Reputation: 40245

There doesn't seem to be difference when using the this keywords. If you have the following code:

class Class3
{
    private long id;

    public void DoWork()
    {
        id = 1;
        this.id = 2;
    }
}

When you run it through reflector you will see the following output:

internal class Class3
{
    // Fields
    private long id;

    // Methods
    public void DoWork()
    {
        this.id = 1L;
        this.id = 2L;
    }
}

Upvotes: 5

TheSoftwareJedi
TheSoftwareJedi

Reputation: 35216

No, that makes no sense at all. Just look at the IL, and kick that developer in the ass.

Also FWIW, I like the underscore in member variables.

Upvotes: 27

Related Questions