C# – the regarded current best practises regarding the “this” keyword in front of field and methods in c#

ccoding-style

Unless it is needed to differentiate between a variable and field with the same name, I never put this. in front of a field or any member access in C#. I see this as no different to m_ prefix that used to be common in C++, and think if you really need to specify that it's a member, your class is too big.

However, there are a number of people in my office that strongly disagree.

What is considered current best practises regarding this.?

EDIT: To clarify, I never use m_ and only use this. when absolutely necessary.

Best Answer

According to the Framework design guidelines, when referring to public or protected fields:

DO NOT use a prefix for field names.

For example, m_ is a prefix.

So, public exposure of a field lends itself to the usage of the this keyword as explained on MSDN

To qualify members hidden by similar names, for example: Copy

public Employee(string name, string alias) 
{
   this.name = name;
   this.alias = alias;
}

When referring to private fields, you can use the m_ if you want. But, for public fields I recommend following the best practices.

Personally, I don't like underscores in my variable names. I also try to be consistent. So, this.name = name; is a good rule of thumb to work in both public/private scenarios.