views:

193

answers:

7

I've decided to use this.variableName when referring to string/int etc.. fields. Would that include ArrayList, ListBox etc too? Like:

private ListBox usersListBox;
private void PopulateListBox()
{
  this.usersListBox.Items.Add(...);
}

...Or not?

And what about classes?

MyClass myClass;
private void PlayWithMyClass()
{
  this.myClass = new MyClass();
  this.myClass.Name = "Bob";
}

? This looks kind of odd to me. And I don't know if I should use this.PublicProperty or only private fields.

I'm not 100% with the C# terminology, but hopefully what I said makes sense.

+6  A: 

I used to do that sort of thing, but now I find that IDEs are pretty smart about giving me a visual indication that I'm dealing with a member variable. I only use "this" when it's necessary to distinguish the member variable from a parameter of the same name.

duffymo
StyleCop will hound you if you omit it, however. One can turn this rule off, but I think I prefer it. The alternative is to distinguish by casing or by a leading underscore ("_"), but I think that requiring the 'this' prefix is not a bad thing. That said, I tend to agree with your statement that it's only necessary when you need "to distinguish the member variable from a parameter of the same name." So, overall, I agree with you, but due to SyleCop's prodding, I've gotten used to always prefixing with 'this', and I do like it at this point -- it certainly makes things 100% clear.
Mike Rosenblum
i have to agree with Mike. I prefix my variables with this. all of the time, because I think it makes it clear to other developers what you are doing. There have been plenty times when I am looking at someones code and wondering where that variable is coming from, if it had "this." it would have been much clearer.
Stan R.
I take the opposite view: putting redundant information in code is something I do only if it's absolutely necessary, and this is a good example of it: when I'm reading my code and I see `this` in it, that's alerting me to the existence of a name collision and warning me that I have to be careful. (I also tend to yield to Resharper's houndings more than to StyleCop's.)
Robert Rossney
+4  A: 

the this. command will allow you to call anything that is in scope in the same class as you are executing. You can access private and public variables and since everything in c# is a object calling a class is the same as calling a string.

You don't have to use this in your code if you don't want to as it is implied in c# unless a method param and a global variable are the same.

AutomatedTester
A: 

using the 'this' keyword can be against any instance of an object. So this means u can use it to reference a class instance (eg. usersListBox, myClass, etc).

It's perfectly fine.

Some people use it to clearly explain what they are referencing so people understand that the instances are in the scope of the code and not external or part of another instance or static member elsewhere.

Finally, you can use it to reference both private and/or public properties and fields and members.

Pure.Krome
A: 

This is nothing more then a keyword pointing to the current instance. In a function, this.foo is generally the same as foo.

As msdn tells you:

The this keyword refers to the current instance of the class.

The page about the this keyword contains a lot more info.

Dykam
+1  A: 

If your class is small enough and does one thing well, then usually you wouldn't need to add this for the sake of readability.

If you can read the whole class easily, what would be the point? It'd be more typing and clutter the code, thus possibly degrade the readability

Sung Meister
+1  A: 

Less is more. Less text to parse is more readable.

I use this in the constructors since my parameters and member variables have the same names (I don't like marking member variables with _).

public class A
{
  int a;
  public A(int a) 
  {  
    this.a = a;
  }
}
Hans Malherbe
A: 

As the this. is implicit you only need to actually use it when disambiguating between class variables and local variables of the same name.

The examples you've given would work how you've written then or like this:

private ListBox usersListBox;
private void PopulateListBox()
{
    usersListBox.Items.Add(...);
}

MyClass myClass;
private void PlayWithMyClass()
{
    myClass = new MyClass();
    myClass.Name = "Bob";
}

it's just a matter of personal preference. If you do choose one over the other, try to be consistent.

ChrisF