2

When upgrading from VS2013 to VS2017, I imported my previous environment settings but I'm encountering this odd editor behavior where the word 'String' is constantly dimmed:

enter image description here

The keyword 'string' is properly colored when in lowercase.

To be clear, it's using the proper color, that is, the color I'd expect when typing a classname as opposed to a keyword. But for some reason, it's dimming it the same way it would dim an unneeded 'using' statement or unreachable code.

Presumably the fix is in Options > Text Editor > C# somewhere but I can't seem to find it.

Festus Martingale
  • 539
  • 1
  • 3
  • 12
  • 1
    Hover over the text and click the lightbulb. Alternatively, put your cursor over the text `String` and press `ctrl+.` (ctrl and a period). It will explain why it greyed out the text. – canton7 Feb 15 '19 at 16:06
  • 1
    Possible duplicate of [Why is "string" considered a simplified version of "String"?](https://stackoverflow.com/questions/32187486/why-is-string-considered-a-simplified-version-of-string) – hardkoded Feb 15 '19 at 16:09

1 Answers1

5

Because it is better practice to use string (lowercase) than String (with a big S). These are the C# types. You will see that "string" is the basic value type. "String" is an alias.The greying out is hinting at this.

If you hover over it, you will get a refactoring recommendation.

enter image description here

Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86
  • better practice because...? I don't disagree but OP might like to know – Dave Feb 15 '19 at 16:13
  • Ah, that would explain it. I have the "Hide Lightbulb Margin" extension installed so may have missed it in the margin but, you're right, when I hover over it, it appears just as you've described. Is there a way to hide this refactoring recommendation IDE0049? – Festus Martingale Feb 15 '19 at 16:14
  • @FestusMartingale See the answer which was linked in a comment on your original question – canton7 Feb 15 '19 at 16:17
  • Hi Canton, I did run across that question before posting my own. The aforementioned "Prefer intrinsic predefined type keyword when declaring locals, parameters and members" doesn't show up in my VS 2017. There *is* a 'predefined type preferences' settings block but oddly, tweaking these settings has no effect on the dimming and further, there seems to be no way to turn off the check completely. – Festus Martingale Feb 15 '19 at 16:21
  • These are the C# types. https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/types-and-variables String is an alias. It's really no biggie, they compile to the same. We follow the default IDE recommendations since they are generally good practice and provide a level of standardisation to our code. – Murray Foxcroft Feb 15 '19 at 16:23
  • 1
    Another way is to add a custom ruleset. [See here](https://learn.microsoft.com/en-us/visualstudio/code-quality/how-to-create-a-custom-rule-set?view=vs-2017). Then you can enable/disable rules individually, change their level (info/warning/error), etc. This particular rule is good for consistency, so I recommend following it. – canton7 Feb 15 '19 at 16:23
  • 1
    @MurrayFoxcroft `string` is the compiler alias, `System.String` is the framework type. – canton7 Feb 15 '19 at 16:26
  • Here is a good post on the nuances between System.String and string: https://stackoverflow.com/questions/7074/what-is-the-difference-between-string-and-string-in-c – Murray Foxcroft Feb 15 '19 at 16:30
  • 1
    Except that since IDE0049 was introduced, I see far fewer people use the framework types for calling methods on, and they use the compiler alias everywhere. – canton7 Feb 15 '19 at 16:50