Why Does Stylecop Recommend Prefixing Method or Property Calls with "This"

Why does StyleCop recommend prefixing method or property calls with this?

It can make code clearer at a glance. When you use this, it's easier to:

  • Tell static and instance members apart. (And distinguish instance methods from delegates.)
  • Distinguish instance members from local variables and parameters (without using a naming convention).

Using this keyword in c# Visual studio vs Stylecop

I prefer not using this. However, since you are using the same naming convention for arguments and class members, it might be hard to differentiate between those two without it.

Since you are using C# I'd recommend this naming convention to avoid cases where you need to use this to explicitly specify which one you mean:

  • _name for private class members
  • Name for public class members, constants and method names
  • name for arguments and local variables

StyleCop's official description for SA1101 is:

A violation of this rule occurs whenever the code contains a call to an instance member of the local class or a base class which is not prefixed with ‘this.’. An exception to this rule occurs when there is a local override of a base class member, and the code intends to call the base class member directly, bypassing the local override. In this case the call can be prefixed with ‘base.’ rather than ‘this.’.

By default, StyleCop disallows the use of underscores or m_ to mark local class fields, in favor of the ‘this.’ prefix. The advantage of using ‘this.’ is that it applies equally to all element types including methods, properties, etc., and not just fields, making all calls to class members instantly recognizable, regardless of which editor is being used to view the code. Another advantage is that it creates a quick, recognizable differentiation between instance members and static members, which are not be prefixed.

A final advantage of using the ‘this.’ prefix is that typing this. will cause Visual Studio to show the IntelliSense popup, making it quick and easy for the developer to choose the class member to call.

I disagree completely with that, but maybe it helps you decide wether to use this or not.

Btw: this has already been discussed StyleCop's Github.

The official recommendation is to disable SA1101.

C# this overuse?

I only use this when it is necessary. Most of the time I don't have the issue of having to differentiate between a local and class variable, so I just don't bother unless the need comes up

When To Use this.method()?

My opinion: use this for disambiguation purposes, e.g., when a parameter name collides with a class property, otherwise leave it out for less noise.

What is a good rule for when to prepend members with 'this' (C#)?

I recommend using Microsoft's guidelines, as verified by StyleCop: http://blogs.msdn.com/sourceanalysis/

The general rule is, prepend members with "this." when they are defined in the class, unless they are static, in which case you cannot.

Here is the rule directly from StyleCop:

SA1101: The call to {method or property name} must begin with the
'this.' prefix to indicate that the item is a member of the class.

C# this overuse?

I only use this when it is necessary. Most of the time I don't have the issue of having to differentiate between a local and class variable, so I just don't bother unless the need comes up



Related Topics



Leave a reply



Submit