Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To Use or Not To Use the 'this' qualifier in C# [duplicate]

Possible Duplicate:
When do you use the “this” keyword?

I have just started using Resharper to analyze my code and there are many things it has suggested that I do and I am really pleased with its output as it is also teaching me a few better methods for doing things.

Obviously its suggestions can be ignored and one of these I would just like to get some feedback from the community on!

I have always preferred to use the 'this' qualifier for properties that belong to the current class (i.e this.Name). No real reason - I just seemed to adopt this many moons ago.

Resharper suggests that this is redundant and maybe I should remove it.

What do you think?

like image 374
EzaBlade Avatar asked May 04 '11 14:05

EzaBlade


People also ask

What is the use of qualifiers in C?

In the C, C++, and D programming languages, a type qualifier is a keyword that is applied to a type, resulting in a qualified type. For example, const int is a qualified type representing a constant integer, while int is the corresponding unqualified type, simply an integer.

What is * Restrict in C?

In the C programming language, restrict is a keyword, introduced by the C99 standard, that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, no other pointer will be used to access the object to which it points.

Why do we require const qualifier in C?

We use the const qualifier to declare a variable as constant. That means that we cannot change the value once the variable has been initialized. Using const has a very big benefit. For example, if you have a constant value of the value of PI, you wouldn't like any part of the program to modify that value.


1 Answers

Preferably, i use this only to prevent ambiguity between (possibly) a property and a function parameter

public class thing
{

   private string name;

   public thing(string name)
   {
       this.name = name; // will set private string name to param string name
   }

}

If you are already working in the context of a certain class, it's not so hard to keep this in mind, and i do not need to be reminded of the fact that I am addressing a local variable every time i address one.

So I think ReSharper's right on this one.

like image 93
Timothy Groote Avatar answered Sep 30 '22 17:09

Timothy Groote