In my previous tutorials, I’d explained the difference between String and StringBuilder, difference between executereader executenonquery and executescalar, difference between dataset datareader dataadapter and dataview and other similar tutorials on Difference, Asp.net here.
In the .NET Framework, at least in the C# language, we have available two types of string class, they are String and string. So, many developers are confused about both types of classes and want to know the difference between them. So, here in this tutorial I’ll explain the difference between String and string in asp.net c# and also explain which one is best and appropriate to use among them.
Here is the example of two different class types:
Example of “string” – [C#]
Example of “String” – [C#]
As you can see the only difference between both the examples is string and String. Right?
.
.
Yes exactly! So technically, there is no difference between both of them. In general, string is an alias for System.String. It’s same like int and System.Int32.
So, now the question raise in our mind is which one is the best and appropriate to use?
Which one is best string or String?
Generally, that’s not the case because using string is equals to System.String. They compile to the same code, so at the execution time there is no difference whatsoever. So, I would say that they are interchangeable and there is no difference when and where you should use one or the other.
But as far as I aware, I recommend you to use string when you’re referring to an object. For example:
And recommend using String when you need to refer specifically to the class. For example:
Few Examples of Aliases in C#
The string and String is one of the aliases in C#. There are several others, here they are:
- using byte is equal to System.Byte
- using bool is equal to System.Boolean
- using short is equal to System.Int16
- using int is equal to System.Int32
- using long is equal to System.Int64
- using double is equal to System.Double
- using decimal is equal to System.Decimal
- using char is equal to System.Char