C# string vs String? What are the Differences?

Objective

As a developer are you confused 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 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.

string s="Hello ";
String st="World";

So, the question is if both strings do the same job then why need to invent another one to the confused programmer. The answer is very simple. The string is just a keyword which provides limited functionality and mainly used in creating a variable. Whereas, System.String is a class which gives you 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 long map to System.Int64, int map to System.Int32 and short map to System.Int16; string map to System.String.

Programming Example

using System;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string s="Hello ";
            string t="World";
            string st=String.Concat(s,t);
            Console.WriteLine(st);
        }
    }
}

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

More Articles

 

Share your thought