Introduction
Many times, developers try to understand what is actually the difference in String vs string in C#. For that, we need to know that C# is a language used with the CLR.
- String is a type in the CLR
- string is a type in C#
- we can't use String without using System; namespace
- string is also known as an alias in C# for String.
So from a technical point of view, there is no difference.
For the best practice in C#, the recommendation is to use string when you refer to the object.
Like: string name = "Smith";
And when we need to refer the class then the recommendation is to use String
Like: string welcomeName = String.Format("Welcome {0}!", name);
If we talk about from compilation point of view so they compile the same code and take some time for execution
Key Difference
string is provided by Microsoft so it is a reserved word, while String is just a class name.
This is the reason that string cannot be used as a variable name.
-
- StringBuilder String = new StringBuilder();
-
-
- StringBuilder string = new StringBuilder();
Note
Only string and object are reference types, the other aliases are all value types.
The complete list is:
SN |
Alias |
Type |
1 |
object: |
Object |
2 |
string: |
String |
3 |
char: |
Char |
4 |
bool: |
Boolean |
5 |
byte: |
Byte |
6 |
sbyte: |
SByte |
7 |
short: |
Int16 |
8 |
ushort: |
UInt16 |
9 |
int: |
Int32 |
10 |
uint: |
UInt32 |
11 |
long: |
Int64 |
12 |
ulong: |
UInt64 |
13 |
float: |
Single |
14 |
double: |
Double |
15 |
decimal: |
Decimal |
Thank you for taking your valuable time to read the full article.