One of the common questions that comes up in .NET interviews is “What is the difference between string and string?”. This is a very simple question but can confuse you during the interview process.
Let’s try to remember and check what exactly it is. To briefly answer this question, both are synonymous. String is short for System.String and string is an alias for String. There is practically no difference, both are compiled to the same code (IL Code – System.String) and they are identical at runtime.
Let’s discuss following code:
class Program
{
static void Main(string[] args)
{
string url = "<a href="https://codeconfig.in;">https://codeconfig.in";</a>
String tags = ".net, tips, C#";
}
}
Let’s check now, if you point over either of string
or String
, you will see same message in the tooltip, as they are referring to the same.
Although their representations are different, they refer to the same type when it comes to intermediate language (IL). You can see the same thing using the IL DASM tool.
You must include the System in use when using String, otherwise you will get the following error:
Error Message: The type or namespace name “String” was not found (are you missing a directive or assembly reference?)
While we talk about Common Language Runtime and IL, you can also explore this topic in relation to other coding languages like Visual Basic.
Must read : 11 Effective Debugging Tips for .NET Developer
Module Module1
Sub Main()
Dim mystring As String = "This is VB Code Base"
End Sub
End Module
Here we have also defined a variable using String (like we do in VB), but when it goes into IL, it has the same representation and so does the language.
According to common sense, when we use a string to declare a variable and when you use it as a class name then “String”. (For example, String.Format, String.Compare, etc.)
You can check more article on our website HERE under C# section or you can also check more such stuff on Microsoft Docs too.