1

I'm almost done reading John Skeet's book C# In Depth, Third Edition, and I'm surprised that he doesn't use this.property inside classes. But since it's John Skeet, I'm sure that means there is a good reason for it.

Example (from pg. 466):

class AsyncForm : Form
{
    Label label; 
    Button button;
    public AsyncForm ( )
    {
       label = new Label { Location = new Point(10, 20),
                            Text = "Length" };
       button = new Button { Location = new Point(10, 50),
                             Text = "Click" };
       button.Click += DisplayWebSiteLength;
       Autosize = true;
       Controls.Add(label);
       Controls.Add(button);
    }
    // ... 
}

No this on Autosize and Controls, eh? Shouldn't we use this in order to avoid ambiguity as to whether the variable refers to a member of the class or some variable that is global with respect to the class?

I'm wondering because I want to make sure all the code I write is Skeet-certified?

user5648283
  • 5,913
  • 4
  • 22
  • 32
  • 1
    Well, that's just a syntactic sugar. – Amit Kumar Ghosh Jan 08 '16 at 06:19
  • 2
    I think this could a good reference for you .. http://stackoverflow.com/questions/23250/when-do-you-use-the-this-keyword – Moumit Jan 08 '16 at 06:22
  • 1
    Code style is strictly opinion based question. You can check out existing off-topic discussions like http://stackoverflow.com/questions/23250/when-do-you-use-the-this-keyword for more info. – Alexei Levenkov Jan 08 '16 at 06:23
  • Possible duplicate of [Is this really a simplification?](http://stackoverflow.com/questions/33186954/is-this-really-a-simplification) – Salah Akbari Jan 08 '16 at 06:32
  • @user2946329: That is one clever title. – BoltClock Jan 08 '16 at 06:34
  • @AlexeiLevenkov If I'm asking "What are the arguments against ..." I'm asking for objective criteria of the language – user5648283 Jan 08 '16 at 06:44
  • The linked question list cases you are looking for - if you are looking to find other cases - read C# specification carefully and ask separate specific questions if you find more usages that you have concerns about. – Alexei Levenkov Jan 08 '16 at 07:20

1 Answers1

2

The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.[From MSDN]

AsyncForm class is inheriting Form class. The two property which you are talking about is the property of Form class not the AysncForm class, that's why there is no this keyword used with these two property.

I will give one simple use case for this.

public class Person
{
   public string name;// this is global variable
   public Person(string name)
   {
     //now we have two variable with name 'name' and we have ambiguity. 
     //So when I will use this.name it will use the global variable.

     this.name = name; //will assign constructor name parameter to global name variable. 
    //If I will do name = name, it will use the local variable for assigning the value into 
     //local variable itself because local variable has high priority than global variable.
   }
}

For better readability of code I would suggest to always use '_' in global variable name like.

public string _name;

and inside constructor

_name = name;

All other use of this keyword please check this answer. When do you use the "this" keyword?

Community
  • 1
  • 1
Mukund
  • 1,679
  • 1
  • 11
  • 20