Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to use this. before code? [closed]

Tags:

c#

this

winforms

I sometimes need to go online and find a tutorial for something. I am often finding that some people put code like this:

this.button1.Text = "Random Text";

Then I find code that is just like this:

button1.Text = "Random Text";

Is it better to use the this.whatever or does it not matter?

like image 880
Dozer789 Avatar asked Nov 27 '22 04:11

Dozer789


1 Answers

It depends. Here's an example class:

class A
{
    private int count;
    public A(int count)
    {
        this.count = count;
    }
}

In this case, the "this." is mandatory because it disambiguates the reference on the left of the assignment. Without it, it is not clear to you reading the code whether "count" would refer to the parameter or the field. (It is clear to the compiler, which has rules to follow.) But in most cases, it is purely a matter of preference.

like image 86
David M Avatar answered Dec 15 '22 09:12

David M