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.

01 CodeConfig back to basic string vs string in c - https://codeconfig.in

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.

11 CodeConfig back to basic string vs string in c - https://codeconfig.in

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
21 CodeConfig back to basic string vs string in c - https://codeconfig.in

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.

new code 1 - https://codeconfig.in

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can