23

So I have a pretty simple class with an Id field, and the Id can be set in the constructor.

Typically I will use this to clearly identify the class property as opposed to the method argument. To me this seems clearer.

IDE0003 wants me to remove the this, with the message 'Name can be simplified', is that correct?

This seems less clear to me, and also allows a casing error to easily result in id = id.

enter image description here

James Wood
  • 17,286
  • 4
  • 46
  • 89
  • 19
    Well, I think you already said it all: Not every 'simplification' is also a 'clarification'.. – TaW Oct 17 '15 at 13:08
  • 1
    i think they both compile into same thing. so you just simplify what you have written nothing else. – M.kazem Akhgary Oct 17 '15 at 13:19
  • 5
    Hmm, these kind of questions only ever seem to be asked by programmers that use a black editor window background. Yeah, that lightbulb is pretty visible. Much easier to not be bothered by it when the background is light :) Sure it is a "simplification", there's less code. But it makes absolutely no difference at runtime and it sure doesn't help you use auto-completion. – Hans Passant Oct 17 '15 at 13:19
  • 2
    @Hans I use dark themes for my editors and hereby execute my right to feel offended. ;) – CodeCaster Oct 17 '15 at 13:37
  • Perhaps, @Hans meant it as a harmless jibe? – Eniola Dec 02 '15 at 21:45
  • 1
    Yes, at Microsoft. Heck of an oversight, wasn't it? – Hans Passant Dec 02 '15 at 21:51

4 Answers4

36

This other question has an answer that says you can configure the editor to remove the behavior. Personally I like "this"

Tools > Options > Text Editor > C# > Code Style and check Qualify member access with 'this'

Visual Studio 2015 - Change Light Bulb, Quick Action settings

Community
  • 1
  • 1
maddoxej
  • 1,662
  • 13
  • 19
  • 10
    Using the keyword `this` explicitly reveals where the variable exists. If it's not prefixed with `this`, `base`, or the name of the class (for static methods), the variable's scope is local and as global as a method. This makes the intention and readability more clear than its omission. – ShooShoSha Nov 30 '16 at 00:36
9

The this keyword almost always is unnecessary, see When do you use the "this" keyword?.

allows a casing error to easily result in id = id

That will yield another warning on its own:

Assignment made to same variable; did you mean to assign something else?

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 3
    C# is case sensitive. In this case it would indeed be a simplification to remove This. – Dieter B Oct 17 '15 at 13:10
  • 2
    @DieterB, if C# is case sensitive (as it is) then what would indeed be a simplification would be removing `this`, not `This` :p – Carvo Loco Jun 20 '18 at 09:42
  • Just out of curiosity, are there any articles from Microsoft that have some justification as to why rules like this were enabled by default? E.g., why didn't they just have the default ruleset accept anything, since VS 2013 didn't enforce or pay attention to any particular style? – jrh Jun 26 '18 at 20:57
6

If you use General Naming Conventions then the this keyword is redundant because the parameter should be id and the property should be Id based on Naming Guidelines. So it seems clear:

public int Id 
{ 
   get; 
   private set; 
}


public VSOMessage(int id)
{
    Id = id;
}

Please note that the guidelines itself don't say, to use or not use this keyword but because C# is case sensitive, it would be a simplification to remove this keyword but when you don't use Naming Conventions then you may naming the property id instead ofId so you should use this keyword in such cases.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 4
    The keyword [`this`](https://msdn.microsoft.com/en-us/library/dk1507sz.aspx) is explicitly useful for this scenario: "To qualify members hidden by similar names". Besides, not every member variable is a property and some properties may have a backing variable (especially useful for immutable objects). Additionally using `this`, `base`, and class name for static methods improves readability and solidifies intention. – ShooShoSha Nov 30 '16 at 00:44
2

If you want to prevent the warning in code rather then updating Visual Studio settings, use the SuppressMessage data annotation, which will prevent the warning.

It looks something like this:

[SuppressMessage("Reason #Enter whatever you'd like", "ID, must match what intellsense is showing it looks something like this: IDE0001", Justification = "(optional, your own description")]

Here is an exact example for your "this" variable situation:

[SuppressMessage("IntelliSenseCorrection", "IDE0003", Justification = "Allowing usage of 'this' keyword to maintain consistency/readability of code.")]
Zachary Rude
  • 13
  • 1
  • 4
TroySteven
  • 4,885
  • 4
  • 32
  • 50
  • Note that this must be applied to the function declaration, not the "offending" statement. Attempting to apply it to the statement raises "CS7014: Attributes are not valid in this context". This was the solution I was looking for, however. Thanks! – Guillermo Prandi Aug 20 '20 at 15:20