Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between String and string in C#?
In C#, string and String are functionally identical − string is simply an alias for System.String. Both refer to the same .NET type and can be used interchangeably in your code.
Syntax
Both declarations are equivalent −
string str1 = "Hello World"; String str2 = "Hello World";
Both can use static methods from the String class −
string result1 = string.Format("Hello {0}", name);
string result2 = String.Format("Hello {0}", name);
Key Differences
| string (lowercase) | String (uppercase) |
|---|---|
| C# keyword and alias | Actual .NET class name |
| Works only in C# | Works in all .NET languages |
| Preferred by C# coding conventions | Used when referencing static methods |
| Cannot be used as identifier name | Can be used as variable name (not recommended) |
Using string for Variable Declaration
When declaring variables, string (lowercase) is the preferred convention in C# −
using System;
class Program {
public static void Main() {
string message = "Welcome to C#!";
string user = "Alice";
Console.WriteLine(message);
Console.WriteLine("User: " + user);
}
}
The output of the above code is −
Welcome to C#! User: Alice
Using String for Static Methods
When calling static methods, both work identically, but String is commonly used −
using System;
class Program {
public static void Main() {
string user = "John";
string formatted1 = String.Format("Hello, {0}!", user);
string formatted2 = string.Format("Welcome, {0}!", user);
Console.WriteLine(formatted1);
Console.WriteLine(formatted2);
Console.WriteLine("Are they equal? " + (formatted1.GetType() == formatted2.GetType()));
}
}
The output of the above code is −
Hello, John! Welcome, John! Are they equal? True
C# Built-in Type Aliases
C# provides aliases for many .NET types for convenience −
| C# Alias | .NET Type | C# Alias | .NET Type |
|---|---|---|---|
| object | System.Object | string | System.String |
| bool | System.Boolean | char | System.Char |
| 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 |
Best Practice Example
using System;
class Program {
public static void Main() {
// Use 'string' for variable declarations
string firstName = "Alice";
string lastName = "Smith";
// Both work for static methods
string fullName = String.Concat(firstName, " ", lastName);
string upperName = string.IsNullOrEmpty(fullName) ? "" : fullName.ToUpper();
Console.WriteLine("Full Name: " + fullName);
Console.WriteLine("Upper Case: " + upperName);
}
}
The output of the above code is −
Full Name: Alice Smith Upper Case: ALICE SMITH
Conclusion
There is no functional difference between string and String in C# − they both refer to System.String. The general convention is to use string for variable declarations and either form for static method calls, with consistency being more important than the specific choice.
