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.
In this blog we shall see the difference between string and String. Well technically both of these are the same but with a slight difference. While “string” is a datatype, whereas “String” represents a class. “string” is an alias for System.
Essentially, there is no difference between string and String (capital S) in C#. String (capital S) is a class in the . NET framework in the System namespace.
String Formatting In C#, the string Format method is used to insert the value of the variable or an object or expression into another string. By using the string. Format method, we can replace the format items in the specified string with the string representation of specified objects.
It is an object of System. String class. C# allows the users to perform different operations on a string such as a substring, trim, concatenate, etc. The string can be declared by using the keyword string which is an alias for the System.
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
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()
Because it doesn't require using System;
at the top.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With