Why is "string" considered a simplified version of "String"?
C#StringVisual Studio-2015C# Problem Overview
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.
C# Solutions
Solution 1 - C#
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
Solution 2 - C#
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()
Solution 3 - C#
Because it doesn't require using System;
at the top.
Solution 4 - C#
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);