TechieClues TechieClues
Updated date Jul 11, 2021
In this blog, we will see the difference between String and string in C# with examples. The String is a class in the .NET framework in the System namespace and the string is an alias of System.String.
  • 1.4k
  • 0
  • 0

Basically, there is no difference between String and string in C#. The String is a class in the .NET framework in the System namespace and the string is an alias of System.String. Both the String and string are compiled to System.String in IL (Intermediate Language), so there is no difference between them.

In the below example, you can see both String and string types return the full name type as System.String.

class StringCompareSample
{
	static void Main(string[] args)
	{
		String laptop1 = "Dell";
		string laptop2 = "HP";
		Console.WriteLine("Type:");
		Console.WriteLine("-------------");
		Console.WriteLine(laptop1.GetType().FullName); 
		Console.WriteLine(laptop2.GetType().FullName); 
		Console.ReadKey();
	}
}  

Output:

Type:
-------------
System.String
System.String

You can use anyone these types (System.Stringor System.string), the string is a keyword and most developers use System.string to declare variables in C#. System.String is a class and commonly used for accessing string static methods like String.IsNullOrEmpty(), String.Format(), String.Compare(), String.CompareTo() etc.

In order to use String or string classes, you need to import System namespace in your *.cs file. Ideally, when you add the *.cs file in your project in Visual Studio, the System namespace will be added automatically, so you need to worry about this and don't need to add this again if already present. You can directly use the String or string class in your *.cs file.

In the below example, the string is used to declare a variable but the String is used to access the static method String,Concat() to concat the two string values.

class StringCompareSample
{
	static void Main(string[] args)
	{
		// decalare variables
		string str1 = "My ";
		string str2 = "Laptop";                       
		// access static method
		string concat = String.Concat(str1, str2);  
		Console.WriteLine(concat);
		Console.ReadKey();
	}
}

Output:

My Laptop

In the .Net framework, alias names are used for different types as shown below,

Alias Type .NET Type
byte System.Byte
sbyte System.SByte
int System.Int32
uint System.UInt32
short System.Int16
ushort System.UInt16
long System.Int64
ulong System.UInt64
float System.Single
double System.Double
char System.Char
bool System.Boolean
object System.Object
string System.String
decimal System.Decimal
DateTime System.DateTime

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!