string vs System.String in C#
This is one of the frequently asked C# interview question for the beginners. This is very simple but confusing question during interview. In this article I will explain difference between string and System.String in C#.
Actually there is no difference between
string is just an alias created by Microsoft for
string stands for
System.String. This alias name is provided so that you don’t need to type every time
System.String, you can simply use string in place of it.
Both of them compiled into same code (IL Code – System.String) and within same during execution time.
Let’s understand by an example:
In this example I have taken 2 variables, one is
string type and second is
System.String type. When I hover mouse on string keyword, it specifies that it refers to System.String.
String instead of
System.String, you need to include
System namespace by using keyword.
Not let’s have a look in Intermediate Language (IL) for this code:
You can see in image, both are same in Intermediate Lanaguage (IL) as well.
String are not alone, Microsoft has provided similar aliases for other C# data type as well:
|Alias Name||C# Data Type|