String vs string in C#: what's the difference and which to choose?

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

String and string in C#: the same thing or not? Analysis with examples

\n\n

If you've ever written code in C#, you've probably noticed that strings can be declared in two ways:

\n\n
string name = "Anna";\nString greeting = "Hello!";
\n\n

And 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\n

Spoiler: 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\n

Short answer: string is an alias for System.String

\n\n

In 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.

\n\n

When the compiler sees string, it automatically substitutes System.String. This means both declarations compile into the same IL code:

\n\n
// These two lines are absolutely identical at the IL level\nstring a = "Hello";\nSystem.String b = "World";
\n\n

If you open a compiled .dll or .exe through ILSpy or dotPeek, you will not find any difference between these two declarations.

\n\n

But why do both options exist?

\n\n

Historically, 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.

\n\n

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.

\n\n

When to use string and when to use String?

\n\n

The golden rule that most teams follow and that Microsoft recommends in the C# documentation:

\n\n
    \n
  • Use string when declaring variables, method parameters, return values — everywhere this keyword works as a type.
  • \n
  • Use String only when calling static methods of the class (e.g., String.IsNullOrEmpty()) or when using System.String in those rare cases where you need to explicitly specify the namespace.
  • \n
\n\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.

\n\n

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\n

Is there a difference in performance?

\n\n

Many developers wonder: maybe string works faster than String? Or vice versa?

\n\n

Answer: there is no difference in performance. None. Absolutely. After compilation, both options turn into the same IL code. The time is

Blogs

Book Recommendations