Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "string" considered a simplified version of "String"?

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.

like image 375
vaindil Avatar asked Aug 24 '15 16:08

vaindil


People also ask

What is difference between string and string?

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.

What's the difference between string and string in C#?

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.

Why do we use string format in C#?

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.

What are string methods in C#?

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.


4 Answers

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

like image 118
SLaks Avatar answered Oct 10 '22 00:10

SLaks


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()

like image 39
ono2012 Avatar answered Oct 09 '22 23:10

ono2012


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

like image 21
John Gietzen Avatar answered Oct 10 '22 01:10

John Gietzen


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);
like image 8
riley Avatar answered Oct 10 '22 01:10

riley