String Vs string in C#

Introduction

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

System namespace not imported - String Vs string in c# 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.

Best Practice

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

Similar Aliases

C# alias for CLR datat types

object System.Object
string System.String
bool System.Boolean
byte System.Byte
sbyte System.SByte
short System.Int16
ushort System.UInt16
int System.Int32
uint System.UInt32
long System.Int64
ulong System.UInt64
float System.Single
double System.Double
decimal System.Decimal
char System.Char
Share this post :
  •  
  •  
  •  
  •  
  •  
  •  

Post A Reply