What is the difference between String vs string? ( C# interview questions) ?
This is one of the most asked C# interview question to confused C# and .NET developers. I do understand that this question does not judge capability of a C# developer , but then when interviewers are asking it we need to have some answer ready for it.
You can also see the below video which is available in DotnetSpider video channel which explain String vs String.
"String" is an alias ( the same thing called with different names) of "string". So technically both the below code statements will give the same output.String s = "C# interview questions";
orstring s = "C# interview questions";
In the same way there are aliases for other c# data type as shown below:-
object: System.Object
string: System.String
bool: System.Boolean
byte: System.Byte
sbyte: System.SByte
short: System.Int16
ushort: System.UInt16
int: System.Int32
uint: System.UInt32
long: System.Int64
ulong: System.UInt64
float: System.Single
double: System.Double
decimal: System.Decimal
char: System.CharSo when both mean the same thing why are they different?
When we talk about .NET there are two different things one there is .NET framework and the other there are languages ( C# , VB.NET etc) which use that framework.
"System.String" a.k.a "String" ( capital "S") is a .NET framework data type while "string" is a C# data type.So when to use "String" and "string"?
First thing to avoid confusion use one of them consistently. But from best practices perspective when you do variable declaration it's good to use "string" ( small "s") and when you are using it as a class name then "String" ( capital "S") is preferred.
In the below code the left hand side is a variable declaration and it declared using "string". At the right hand side we are calling a method so "String" is more sensible.string s = String.ToUpper() ;
Thank you very much