In C# you can refer to values in a class using the ‘this’ keyword.
class MyClass { private string foo; public string MyMethod() { return this.foo; } }
While I presume the answer will likley be user preference, is it best practice to use the this keyword within a class for local values?
In the spirit of DRY, I would say this is not a particularly useful practice in general. Almost any use of
thiscan be shortened to an equivalent expression by just removing thethis.One exception is if you have a local parameter which happens to have the same name as another class member; in that case you must distinguish between the two with
this. But this is a situation you can easily avoid, by simply renaming the parameter.