Essentially, there is no difference between string and String (capital S) in C#.
String (capital S) is a class in the .NET framework in the System namespace. The fully qualified name is System.String. Whereas, the lower case string is an alias of System.String.
Consider the following example.
string str1= "Hello";
String str2 = "World!";
Console.WriteLine(str1.GetType().FullName); // System.String
Console.WriteLine(str2.GetType().FullName); // System.String
As you can see in the above example, the full name of both types are System.String. So, this proves that both are same.
It is recommended to use string (lower case) over String. However, it's a matter of choice. You can use any of them. Many developers use string to declare variables in C# and use System.String class to use any built-in string methods e.g., String.IsNullOrEmpty().
Please note that you must import System namespace at the top of your .cs file to use String class, whereas string keyword can be used directly without any namespace.
.NET includes different aliases for different types. The following table lists data type aliases.
Alias | .NET Type | Type |
---|---|---|
byte | System.Byte | struct |
sbyte | System.SByte | struct |
int | System.Int32 | struct |
uint | System.UInt32 | struct |
short | System.Int16 | struct |
ushort | System.UInt16 | struct |
long | System.Int64 | struct |
ulong | System.UInt64 | struct |
float | System.Single | struct |
double | System.Double | struct |
char | System.Char | struct |
bool | System.Boolean | struct |
object | System.Object | Class |
string | System.String | Class |
decimal | System.Decimal | struct |
DateTime | System.DateTime | struct |
- How to validate email in C#?
- How to get the sizeof a datatype in C#?
- Difference between String and StringBuilder in C#
- Static vs Singleton in C#
- Difference between == and Equals() Method in C#
- Asynchronous programming with async, await, Task in C#
- How to loop through an enum in C#?
- Generate Random Numbers in C#
- Difference between Two Dates in C#
- Convert int to enum in C#
- BigInteger Data Type in C#
- Convert String to Enum in C#
- Convert an Object to JSON in C#
- Convert JSON String to Object in C#
- DateTime Formats in C#
- How to convert date object to string in C#?
- Compare strings in C#
- How to count elements in C# array?
- How to get a comma separated string from an array in C#?
- Boxing and Unboxing in C#
- How to convert string to int in C#?