string vs String in C#: is there any difference?
There is no difference in behaviour: string is a C# keyword that is an alias for the .NET type System.String. Both spellings compile to exactly the same thing. The choice is a matter of style, with one or two technical corners.
Same type
string a = "hello";
String b = "hello"; // needs 'using System;' (or global usings)
System.String c = "hello";
Console.WriteLine(a.GetType() == typeof(String)); // True
Console.WriteLine(typeof(string) == typeof(System.String)); // True
Static members are reachable through either name: string.IsNullOrEmpty(x) and String.IsNullOrEmpty(x) are the same call.
Where the spellings differ
- Scope.
stringis a keyword and always meansSystem.String.Stringis an ordinary type name, resolved throughusingdirectives; in theory a different type calledStringin your namespace would take precedence. - Without
using System;,Stringdoes not compile,stringdoes. Modern SDK projects add implicit global usings, so this rarely bites. - Keyword contexts. Only the keyword works where the language expects one, for example in some older syntaxes such as an enum's underlying type (
enum E : intrequires the keyword forint).
The other aliases
| C# keyword | .NET type |
|---|---|
string | System.String |
object | System.Object |
bool | System.Boolean |
int / long / short / byte | Int32 / Int64 / Int16 / Byte |
double / float / decimal | Double / Single / Decimal |
char | Char |
nint / nuint | IntPtr / UIntPtr |
The .NET name is useful in method names that describe a type, as the framework itself does: Convert.ToInt32, reader.GetInt64, not ToInt.
Which to use?
Microsoft's C# coding conventions recommend the language keywords (string, int) rather than the runtime type names for declarations and member access, and the default code-style rule IDE0049 in Visual Studio suggests the same. Some teams prefer String.Format for static calls; what matters is consistency within a code base, enforced by an .editorconfig setting.
A more substantial question about strings is mutability: see string vs StringBuilder.
A note on VB.NET
Visual Basic has the same arrangement: String (with a capital letter, as VB keywords are written) is a keyword alias for System.String, just as Integer maps to System.Int32. Code that mixes C# and VB.NET projects therefore uses one type throughout; only the spelling differs between the languages. In both, string comparison rules (ordinal, culture-aware, case-insensitive) matter far more in practice than which alias you type: pass a StringComparison value to Equals, StartsWith and IndexOf to make the intent explicit.
Official documentation
- Microsoft Learn: Built-in types (C# reference), The string type
- Microsoft Learn: C# coding conventions, IDE0049