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 string do same job then why need to invent other one to confused programmer. Answer is very simple. String is just a keyword which provides limited functionality and mainly used in creating variable. Whereas, System.String is a class which gives you rich set of functions and properties to manipulate string.
Known Difference between string and System.String
- string is a keyword and widely used for declaring variable. Where as System.String is a class and used for accessing string static methods like String.Format(), String.Compare() etc.
- 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.
- As
longmap toSystem.Int64,intmap toSystem.Int32andshortmap toSystem.Int16;stringmap toSystem.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.