Difference Between C# String Vs String

In this article, we will see the differences between string and System.String

Objective

As a developer, are you confused about how to choose between string and System.String? In this article, I am going to show you all the differences between string and System.String in C# with proper programming example.

WHAT IS THE DIFFERENCE BETWEEN STRING AND STRING() IN C#?

Basically, there is no difference between string and String in C#. "string" is just an alias of System.String and both are compiled in the same manner. String stands for System.String and it is a .NET Framework type. "string" is an alias in the C# language for System.String. Both of them are compiled to System.String in IL (Intermediate Language), so there is no difference.

  1. string s="Hello ";       
  2. String st="World";  

So, the question is if both strings do the same job, then why do we need to invent another one for the confused programmer? The answer is very simple. "string" is just a keyword which provides limited functionality and is mainly used in creating a variable. Whereas, System.String is a class which gives you a rich set of functions and properties to manipulate the string.

KNOWN DIFFERENCE BETWEEN STRING AND SYSTEM.STRING

  1. string is a keyword and widely used for declaring a variable. Whereas System.String is a class and used for accessing string static methods like String.Format(), String.Compare() etc.
  2. You can use string directly in your program and you don’t need to add Using System namespace at the beginning of your program whereas to using System.String you need to import Using System namespace.
  3. As a long map to System.Int64, int maps to System.Int32 and short map to System.Int16; string map to System.String.
PROGRAMMING EXAMPLE 
  1. using System;  
  2. namespace ConsoleApplication {  
  3.     public class Program {  
  4.         public static void Main(string[] args) {  
  5.             string s = "Hello ";  
  6.             string t = "World";  
  7.             string st = String.Concat(s, t);  
  8.             Console.WriteLine(st);  
  9.         }  
  10.     }  
  11. }  

Here, you can see that string is used for declaring variable whereas String is used for accessing the static method String.Concat() that joins two strings together.

Hopefully, this clears up any issues!