Share via


Solved: Confusion between string and String in C#

Almost a decade ago when I was in school and started programming in C#, my first task was to work on a legacy code where my predecessors took the liberty to mix and match the usage of 'String' and 'string' in the code. I used to be confused and stuck to using 'string' for my part of the code till I finally got over the confusion between these two. But every now and then I would hear from a new-comer to C# that what's the difference between 'String' and 'string' in C#.

The Difference and No Difference

Ok, here's the truth between these two. Drum roll!......
There is no difference.

string is just an alias of System.String Class i.e. String.

Well, that was short. But let me demonstrate few easy steps to overkill this easy understanding.

As the documentation says, a String object is a sequential collection of System.Char object that represents a text (or string) and is a reference type. 'string' is just an alias of this String class, which means whether you use 'string' or 'String' in your C# code, they are the same or referring to the same object type.

Let's assume we define the following in our code:

using System;
 
String bigStringType = "I am a big string type";
string smallStringType = "I am a small string type";

Now, if we assign them the same value as follows:

// Giving them same value
bigStringType = smallStringType = "Just a default string value";

We will see that the value stored in each of these variables are same and equal.

// Comparing using .Equals() method
if (bigStringType.Equals(smallStringType)) // True
         Console.WriteLine("bigStringType & smallStringType matched using Equals() method of System.String class");

This uses the infamous Equals() method to compare the values contained in each of these String variables.

But majority of the times we also see developers using '==' operator to compare the values of two Strings as well, which is mostly exhibited by value type, not a reference type. Although this confuses some developers that why String has both '==' and Equals() method to compare the stored values but we need to keep in mind that at the end of the day String is still reference type, which is capable of holding huge values demanding to be stored on the heap. 

// Comparing using '==' operator
if (bigStringType == smallStringType) // True
      Console.WriteLine("bigStringType & smallStringType matched using '==' operator");

I believe that it is ingeniosity of the C# development team at Microsoft who made the String object to be used easily in various ways. But do prove me wrong by providing your point of view and reference.

Now, back to clearing confusion between string and String, if we use GetType() and GetHashCode() methods to check the types and the value stored in the aforementioned variables, we can see that they are exactly the same, hence proving the point without taking anyone else's word into account.

// Comparing th types of bigStringType and smallStringType
if (bigStringType.GetType() == smallStringType.GetType())
Console.WriteLine("Even the types of bigStringType & smallStringType are equal, proved by .GetType() method \n" +
 "The returned type of bigStringType & smallStringType is {0}", bigStringType.GetType());

// Overkilling the confusion by unnecessarily proving that
// even the hash of values of bigStringType & smallStringType are equal as well
if (bigStringType.GetHashCode() == smallStringType.GetHashCode())
Console.WriteLine("The hash code for the value of bigStringType & smallStringType are equal as well");

Extras

You can download the code snippet from Technet Gallery to see the aforementioned code in action.