string
String
c#

What is the difference between String and string in C#?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, string and String refer to the same .NET type: System.String. The difference is mostly about language syntax and coding style, not runtime behavior.

That said, the distinction still matters because it affects readability, consistency, and how people interpret your code. Most confusion comes from mixing C# keywords with framework type names.

They Point to the Same Type

string is a C# keyword. String is the framework type name, short for System.String. The compiler treats them as the same type.

You can prove that directly:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        Console.WriteLine(typeof(string) == typeof(String));
8        Console.WriteLine(typeof(string) == typeof(System.String));
9    }
10}

Both lines print True.

That means there is no performance difference, memory difference, or feature difference between string and String. If you declare a variable with either spelling, the runtime type is still System.String.

Why C# Has Both Spellings

C# includes keyword aliases for several built-in framework types:

  • 'int for System.Int32'
  • 'bool for System.Boolean'
  • 'object for System.Object'
  • 'string for System.String'

The aliases make code look like a language rather than a library lookup. That is why idiomatic C# typically uses string in variable declarations, method parameters, and return types.

csharp
1public string FormatName(string first, string last)
2{
3    return first + " " + last;
4}

The same code can be written with String, but most C# style guides would consider that less natural:

csharp
1public String FormatName(String first, String last)
2{
3    return first + " " + last;
4}

Both compile to the same thing. The difference is style.

When String Still Appears Often

You still see String in a few places.

One is when reading .NET documentation, because the framework type is documented as System.String. Another is when calling static members, where some developers prefer the type name:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        string text = "hello";
8        Console.WriteLine(String.IsNullOrWhiteSpace(text));
9        Console.WriteLine(string.IsNullOrWhiteSpace(text));
10    }
11}

Both calls are valid because static members are available through either name. Many teams still prefer string.IsNullOrWhiteSpace for consistency with the keyword alias.

You may also see String when code is auto-generated, translated from another .NET language, or written by developers who want to emphasize the framework type explicitly.

What the Style Guidance Usually Is

The usual C# convention is simple:

  • use string in declarations and ordinary code
  • use framework type names like StringBuilder, DateTime, or CultureInfo when no keyword alias exists
  • stay consistent within the codebase

That convention matches the wider pattern of using int, bool, and string rather than Int32, Boolean, and String.

Consistency matters more than dogma. A codebase that mixes both spellings randomly is harder to scan than one that follows a clear rule.

A separate but important fact is that System.String is immutable. That property is true no matter which spelling you use.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        string a = "cat";
8        string b = a;
9        a += "s";
10
11        Console.WriteLine(a);
12        Console.WriteLine(b);
13    }
14}

This prints cats and then cat because concatenation creates a new string object instead of modifying the old one.

People sometimes assume String and string differ because String looks like a class name and string looks like a primitive. In C#, string is still a reference type, and it still has all the behavior of System.String.

Common Pitfalls

A common mistake is thinking string is a lightweight primitive while String is a heavier object wrapper. That is false in C#. They are the same type.

Another mistake is mixing String and string arbitrarily in the same file. The code still works, but it creates visual noise and makes style look accidental.

Some developers also assume string cannot access static members because it is a keyword. It can. string.IsNullOrEmpty is just as valid as String.IsNullOrEmpty.

Finally, do not confuse this topic with nullable reference types. string versus string? is a separate question about nullability annotations, not about string versus String.

Summary

  • 'string is the C# keyword alias for System.String.'
  • 'String and string compile to the same runtime type.'
  • There is no performance or behavior difference between them.
  • Most C# code uses string for declarations because it is the idiomatic style.
  • The real rule is consistency: pick the convention your codebase follows and use it uniformly.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.