Navigation:  StyleCop 4.3 > StyleCop 4.3 Rules > Readability Rules >

SA1101: PrefixLocalCallsWithThis

Previous pageReturn to chapter overviewNext page
StyleCop 4.3

Send comments on this topic.

SA1101: PrefixLocalCallsWithThis

Glossary Item Box

TypeName
PrefixLocalCallsWithThis

CheckId

SA1101

Category

Readability Rules

 

Cause

A call to an instance member of the local class or a base class is not prefixed with ‘this.’, within a C# code file.

Rule Description

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.

How to Fix Violations

To fix a violation of this rule, insert the ‘this.’ prefix before the call to the class member.

 


© Microsoft Corporation. All Rights Reserved.