When accessing instance variables or properties of a class from within the class itself, do you prepend them with \"this.
\"?
No. I consider it visual noise. I think the this variable is a crutch to bad naming styles. Within my type I should be able to manage the naming of the fields, properties and methods.
There is absolutely no good reason to name your backing field "myfield", the parameter to the constructor as "myField" and the property to be "myField".
public class TestClass
{
private string myField;
public TestClass(string myField)
{
this.myField = myField;
}
public string MyField {get { return myField;} set {myField = value}}
}
Personally, I always add a prefix of _ to all my private backing fields.
public class TestClass
{
private string _myField;
public TestClass(string myField)
{
_myField = myField;
}
public string MyField {get { return _myField;} set {_myField = value}}
}
and now with automatic properties in C#
public class TestClass
{
private string MyField {get; set;}
public TestClass(string myField)
{
MyField = myField;
}
}
Other than the above maybe the only other time you type this. is because you want to see the intellisense for your current type. If you need to do this then I submit that your type is too big and probably not following the Single Responsibility Principle. And lets say you are. Why keep the this. around after you actually make the call. Refactor it out.