String and string in C#: the same thing or not? Analysis with examples
\n\nIf you've ever written code in C#, you've probably noticed that strings can be declared in two ways:
\n\nstring name = "Anna";\nString greeting = "Hello!";\n\nAnd both options work. A reasonable question arises: what is the difference between string and String in C#? Are they different types? Is one faster? Or is it just a quirk of the language developers?
\n\nSpoiler: there is no difference. But it's not as simple as it seems at first glance. Let's really break it down so you not only know the answer to a popular Stack Overflow question but also understand why it's done this way and which option is better to use in real projects.
\n\nShort answer: string is an alias for System.String
\n\nIn C#, the string keyword is simply a type alias for the System.String class. Just like int is an alias for System.Int32, and bool is an alias for System.Boolean.
When the compiler sees string, it automatically substitutes System.String. This means both declarations compile into the same IL code:
// These two lines are absolutely identical at the IL level\nstring a = "Hello";\nSystem.String b = "World";\n\nIf you open a compiled .dll or .exe through ILSpy or dotPeek, you will not find any difference between these two declarations.
\n\nBut why do both options exist?
\n\nHistorically, C# inherited its syntax from C/C++, where string is not a keyword but a class. The .NET developers decided to add convenient aliases to make the code look more natural and familiar for programmers coming from other languages.
Additionally, aliases do not require importing a namespace. To use String, you need to either add using System; or write the full name System.String. With string, there are no such issues — it is always available.
When to use string and when to use String?
\n\nThe golden rule that most teams follow and that Microsoft recommends in the C# documentation:
\n\n- \n
- Use
stringwhen declaring variables, method parameters, return values — everywhere this keyword works as a type. \n - Use
Stringonly when calling static methods of the class (e.g.,String.IsNullOrEmpty()) or when usingSystem.Stringin those rare cases where you need to explicitly specify the namespace. \n
Why is that? Because string is a language keyword, it is highlighted in the IDE and it is immediately clear that it is a string. String is just a class name, and it is easier to confuse it with other classes.
Example of good style
\n\n// Variable declaration (use string)\nstring userName = "Ivan";\n\n// Calling a static method (use String)\nif (String.IsNullOrEmpty(userName))\n{\n Console.WriteLine("Name cannot be empty");\n}\n\n// Method parameter (use string)\npublic void Greet(string name)\n{\n Console.WriteLine($"Hello, {name}!");\n}\n\nIs there a difference in performance?
\n\nMany developers wonder: maybe string works faster than String? Or vice versa?
Answer: there is no difference in performance. None. Absolutely. After compilation, both options turn into the same IL code. The time is