67

In C# I usually use String when I'm utilizing a method and string when declaring a variable. I read elsewhere that this is the preferred method to keep things clean and that made sense to me. In Visual Studio 2015, I'm getting a new message I haven't gotten before when I use String: Name can be simplified. The VS suggestion is to use string instead.

Why is string now preferred over String in VS2015 whereas it wasn't in 2013??

Not a duplicate of this question. That one asks what the difference is overall, I'm asking why VS is now suggesting one over the other; I don't know if a technical difference has changed or something to that effect.

vaindil
  • 7,536
  • 21
  • 68
  • 127
  • Uniformity in the usage is also an important aspect. – Jeroen Vannevel Aug 24 '15 at 16:47
  • Voting to close an opinion-based as there's no technical reason to use one over the other. – D Stanley Aug 24 '15 at 16:49
  • 4
    @DStanley: That's the point of my question. If it is opinion-based then that would answer my question. I asked because VS is suggesting it whereas it hasn't in the past, which led me to believe there's a technical reason for it. – vaindil Aug 24 '15 at 16:50
  • 21
    Valid question in my opinion. Neither a duplicate, nor opinion-based. – displayName Aug 24 '15 at 16:51
  • No, there's not. If your question is "what is the difference" then the duplicate would answer that. – D Stanley Aug 24 '15 at 16:51
  • 1
    @displayName The question "what is the difference" is answered by the duplicate. The question "Why is one preferred over the other" is completely subjective. If the question is "How can I get VS to stop suggesting the change" then SLaks provided a viable answer. – D Stanley Aug 24 '15 at 16:52
  • 2
    Possibly just a consequence of the fact that most people prefer `string` since it stays in line with using `int`, `float` and other aliases. Though this is obviously conjecture – Jeroen Vannevel Aug 24 '15 at 16:53
  • 1
    @DStanley: I agree with you bro. So which of the three questions you mentioned is being asked here? The third one, right? Therefore this is a valid question and not a duplicate/opinion requester. – displayName Aug 24 '15 at 17:09
  • The literal question _was_ "Why is `string` preferred over `String`?" So assuming any other question that that is purely speculative. Now I see the post has been edited to ask a different question. – D Stanley Aug 24 '15 at 17:12
  • @DStanley: I updated the "literal question", take what's _actually_ being asked and not the literal actual question itself. – vaindil Aug 24 '15 at 17:13
  • Given that there's no technical difference between the two, only the VS team can answer the question as to why one setting is the default. Since you can change the behavior there's obviously room for personal preference. – D Stanley Aug 24 '15 at 17:17
  • @DStanley: Though the post has been edited, yet even the first question was also asking as to why does VS suggest so and so wrt strings. The question was valid then too. Let's kill it here man. Its a useless argument. Bottom line is that everyone, including me, should read the question patiently first. – displayName Aug 24 '15 at 17:32
  • 5
    @Vaindil: You question has been marked as duplicate, but I feel it was totally valid question. You have been hunted by the pride of Repo-snobs. :D – displayName Aug 24 '15 at 17:54
  • 2
    @Servy I really don't think this is a duplicate. Would you reconsider and perhaps help me re-open? We could then mark _this_ as a dupe of the far superior http://stackoverflow.com/q/34597973/560648 ... ;) – Lightness Races in Orbit Jan 04 '16 at 19:20

4 Answers4

44

Because you didn't uncheck "Prefer intrinsic predefined type keyword when declaring locals, parameters and members" found under Tools > Options > Text Editor > C# > Code Style

ono2012
  • 4,967
  • 2
  • 33
  • 42
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 4
    It's under Code Style in the C# section, not Advanced. But still, I don't think this is quite correct--the option you proposed changes _variable declarations_ (for example, from `int` to `Int32`). The other similar option, `Prefer intrinsic predefined type keyword in member access expressions`, makes a similar change (`int.MaxValue` to `Int32.MaxValue`). In VS2013 I used `string`/`String` how I described in the OP, never touched these options, no message from VS. Now it's throwing this message and I don't know why. (By the way, VS2013 doesn't even have this option.) – vaindil Aug 24 '15 at 16:59
  • 1
    @Vaindil: Yes; this entire feature is new to Roslyn. – SLaks Aug 24 '15 at 17:37
  • Is this new method preferred for Roslyn then? I assume so if VS throws a warning about it, just want to check. – vaindil Dec 30 '15 at 15:50
  • 4
    @SLaks Will you marry me? Seriously though, this warning is pretty annoying. `ulong.TryParse()` just looks so wrong compared to `UInt64.TryParse()`. – Dan Bechard Jul 12 '17 at 21:10
  • 4
    It's called 'predefined type preferences' in VS 2017. – Der_Meister Nov 27 '17 at 08:20
16

VS2017-2019 Tools > Options > Text Editor > C# > Code Style (>predefined type preferences:) > For member access expressions

select "Prefer framework type"


VS2015 Tools > Options > Text Editor > C# > Code Style

uncheck "Prefer intrinsic predefined type keyword in member access expressions"


Example given in VS2015-2019 for this option flips

var local = int.MaxValue (Prefer predefined type /ticked)

to

var local = Int32.MaxValue (Prefer framework type /unticked)


ReSharper - to disable it/configure the inspection severity, it is the "Replace built-in type reference with a CLR type name or a keyword" rule.

Now nothing hints at me to change String.Format() to string.Format()

ono2012
  • 4,967
  • 2
  • 33
  • 42
  • For Visual Studio 2017 (15.8) the property is: 'predefined type preferences' -> For member access expressions -> Prefer framework type + restart VS – Vadym Kyrylkov Aug 16 '18 at 09:37
12

Because it doesn't require using System; at the top.

John Gietzen
  • 48,783
  • 32
  • 145
  • 190
9

string is an alias in C# for System.String. So technically, there is no difference. It's kinda like int vs. System.Int32.

As far as the what you 'Should' do, string is the preferred object for variables and String for classes as it the practised choice.

usually seen like this

string example = "hello world";

string example = String.Format("Hello World {0}!", example);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
riley
  • 154
  • 6