C sharp: Use prefix ‘this’ instead of underscores to distinguish local variables

Let’s first agree on what we are talking about. The question is how we access instance members from within non-static methods and constructors of a class or one of its sub-classes if visibility modifiers allow doing that.

Underline-notation

  • suggests that you use the “_” prefix in the names of private fields
  • it also says that you should never use “this” unless it’s absolutely necessary

This-notation

  • suggests that you just always use “this.” to access any instance member

Why does this-notation exist?

Because this is how you

  • tell apart a parameter from a field when they share the same name
  • ensure you are working in the context of the current instance

Example

public class Demo
{
   private String name;
   public Demo(String name) {
       this.name = name;
   }
}

Why does underline-notation exist?

Some people don’t like typing “this”, but they still need a way to distinguish a field and a parameter, so this is why they agreed to use “_” in front of a field

Example

public class Demo
{
   private String _name;
   public Demo(String name) {
      _name = name;
   }
}

One may think it’s just the matter of personal taste and both ways are equally good/bad. However there are certain aspects where this-notation beats the underscore-notation:

Clarity

  • underline-notation clutters names
  • this-notation keeps names intact

Consistency

  • underline-notation is inconsistent, it makes you treat fields in a special way, but you cannot use it with other members
  • this-notation is consistent, you don’t have to think, you just always use “this” to refer to any member

Autocompletion

When you need to see the list of instance members:

  • underline-notation doesn’t help you much, because when you type “_” the autocomplete popup shows you the private fields and all types available from the linked assemblies mixed with the rest of the instance members
  • this-notation gives you a clear answer, by typing “this” all you see is the list of members and nothing else

Ambiguity

Sometimes you have to deal with the code without help of the Intellisense. For example when you are doing code reviews or browsing the source code repository online.

  • underline-notation is ambiguous: When you see Something.SomethingElse you cannot tell whether Something is a class and SomethingElse is its static property… or may be Something is a current instance property which has its own property of SomethingElse
  • this-notation is clear: When you see Something.SomethingElse it can only mean a class with a static property and when you see this.Something.SomethingElse you know that Something is a member and SomethingElse is its property

Extension methods

You cannot use extensions methods on the instance itself without using “this.”

  • underline-notation requires that you don’t use “this”, however with the extension methods you have to
  • this-notation saves you from hesitation, you always use “this”, period.

Visual Studio support

  • underline-notation doesn’t have a built-in support in Visual Studio
  • this-notation is supported by Visual Studio naturally: http://www.screenr.com/zDCH

Official recommendations

There a lot of official guidelines that clearly say “do not use underscores” especially in C#

C sharp: Use prefix ‘this’ instead of underscores to distinguish local variables was last modified: January 7th, 2016 by tabcom