In this post I would like to discuss about String Vs string in c#.It is a common interview question for c# beginners.
String vs string
String is a class in .Net library while string is a c# alias or keyword for String class.
String is a .Net Framework date type.Both String and string refers to the same class String in .Net Library. In a c# program you can use both String and string interchangeably. After compilation, both of them will be converted into System.String in IL(Intermediate Language).
string = System.String Class
String is a class in System namespace. so string is equivalent to System.String.
Consider the following code snippet
Above program will run without any problem. But if we remove the line for importing System namespace, declaration of str1 will create some problem like
That means we can’t use String Class in c# programs without importing System namespace but for string keyword we don’t need System namespace.
Actually both string and String refers same class in .Net Library, Even though, there are some recommended practice to use in c#programs.
use string keyword when declaring string variables.
use String if you need to refer specially to the class
C# alias for CLR datat types
1 thought on “String Vs string in C#”