Question

String vs string in C#: What's the Difference?

csharp.netstringtypesalias

Question

In C#, what is the difference between String and string, and which one should be used?

string s = "Hello world!";
String s2 = "Hello world!";

These two declarations appear very similar. Are they different in behavior, performance, or meaning? If not, when should one form be preferred over the other?

Short Answer

By the end of this page, you will understand that string in C# is a language keyword that maps directly to System.String. You will learn why both forms represent the same .NET type, how they are used in real code, and which style is usually preferred for readability and consistency.

Concept

In C#, string and String refer to the same underlying type:

  • string is a C# keyword
  • String usually refers to System.String, the .NET class

That means these are effectively equivalent:

string a = "hello";
System.String b = "hello";

If you have using System; at the top of your file, then you can also write:

String c = "hello";

All three variables above have the same type: System.String.

Why this exists

C# provides built-in aliases for some common .NET types to make code easier to read and write. For example:

  • int -> System.Int32
  • bool -> System.Boolean
  • string -> System.String
  • object -> System.Object

So string is not a different kind of string. It is simply the C# keyword alias for the .NET System.String type.

Does it affect performance?

No. There is no performance difference between string and String.

Does it affect behavior?

No. They behave the same because they are the same type.

Which one should you use?

In most C# code, developers prefer:

  • string when declaring variables, parameters, return types, and fields
  • String only when calling static members directly, such as String.IsNullOrEmpty(...)

Example:

string name = "Ada";

if (String.IsNullOrEmpty(name))
{
    Console.WriteLine("Missing name");
}

Some teams also prefer string everywhere and write:

if (string.IsNullOrEmpty(name))
{
    Console.WriteLine("Missing name");
}

Both are valid in modern C# because static members can be accessed through the alias too.

Why this matters in real programming

Understanding this helps you:

  • read C# codebases with confidence
  • follow style conventions consistently
  • avoid thinking these are two separate types
  • understand other aliases like int, bool, and object

Mental Model

Think of string and System.String like two labels on the same box.

  • The box is the actual .NET type that stores text
  • string is the short C# label
  • System.String is the full official label

They do not represent different boxes. They both point to the same thing.

A similar idea exists in everyday language:

  • TV and television mean the same object
  • one is shorter and more convenient
  • the other is the formal name

In C#, string is like saying TV, while System.String is like saying television.

Take Quiz

Syntax and Examples

Basic syntax

string message = "Hello";
System.String message2 = "Hello";

If using System; is in scope:

String message3 = "Hello";

These all create variables of the same type.

Example: proving they are the same type

using System;

class Program
{
    static void Main()
    {
        string a = "hello";
        String b = "world";

        Console.WriteLine(a.GetType());
        Console.WriteLine(b.GetType());
    }
}

Output:

System.String
System.String

Example: common style

using System;

class Program
{
    static void Main()
    {
        string name = "Mina";

        if (string.IsNullOrEmpty(name))
        {
            Console.WriteLine("Name is empty");
        }
        else
        {
            Console.WriteLine(name);
        }
    }
}

Explanation

  • string name declares a text variable
  • string.IsNullOrEmpty(name) calls a static helper on the same string type
  • even though string looks like a keyword, it still maps to System.String

Step by Step Execution

Consider this example:

using System;

class Program
{
    static void Main()
    {
        string first = "Hello";
        String second = "World";

        Console.WriteLine(first.GetType() == second.GetType());
    }
}

Step by step

  1. string first = "Hello";

    • A variable named first is created.
    • Its type is System.String through the string alias.
  2. String second = "World";

    • A variable named second is created.
    • Because String refers to System.String, it has the same type.
  3. first.GetType()

    • Returns the runtime type of first.
    • This is System.String.
  4. second.GetType()

    • Returns the runtime type of second.
    • This is also System.String.
  5. == compares the two type objects.

    • They match.
    • The result is True.

Output:

True

This confirms that string and String are not different runtime types.

Real World Use Cases

1. Declaring text data in applications

string username = "alice";
string email = "alice@example.com";

This is the most common use in web apps, desktop apps, and APIs.

2. Validating input

if (string.IsNullOrWhiteSpace(email))
{
    Console.WriteLine("Email is required.");
}

String validation is common in forms, request handling, and configuration loading.

3. Working with API data

string json = "{ \"name\": \"Sam\" }";

APIs often send and receive textual data such as JSON, URLs, headers, and tokens.

4. Logging and messages

string logMessage = "User logged in successfully.";

Most logs, status updates, and error messages are strings.

5. Configuration values

string connectionString = "Server=localhost;Database=AppDb;";

Environment variables and configuration files often produce string values first.

Take Quiz

Real Codebase Usage

In real C# projects, developers usually follow style conventions rather than technical necessity, because string and String are the same type.

Common pattern: use aliases for built-in types

Most codebases prefer aliases like:

  • string
  • int
  • bool
  • object

This makes everyday declarations shorter and more familiar.

public string GetDisplayName(User user)
{
    return user.FirstName + " " + user.LastName;
}

Validation and guard clauses

Strings are often validated early:

public void SaveUser(string name)
{
    if (string.IsNullOrWhiteSpace(name))
    {
        throw new ArgumentException("Name is required.");
    }

    // continue processing
}

Configuration and parsing

Many values start as strings before conversion:

string portText = "8080";
int port = int.Parse(portText);

Error handling and messaging

catch (Exception ex)
{
    string message = ex.Message;
    Console.WriteLine(message);
}

Team consistency matters most

A real codebase should choose a style and apply it consistently. If the team's style guide says use C# aliases, use string. If a project uses String in specific places, follow that convention.

Common Mistakes

Mistake 1: Thinking string and String are different types

Broken assumption:

string a = "x";
String b = "y";
// These are different types  <-- incorrect idea

Reality:

  • both are System.String
  • there is no type difference

Mistake 2: Forgetting that String may need a namespace

This may fail if using System; is missing:

String name = "Ali";

Safer alternatives:

using System;
String name = "Ali";

or

string name = "Ali";

or

System.String name = "Ali";

Mistake 3: Mixing styles randomly

This is valid but less readable:

string firstName = "Eva";
String lastName = "Stone";
string city = "Paris";

Try to stay consistent within a project.

Mistake 4: Confusing string with mutable text

Beginners sometimes expect strings to change in place.

string text = "Hi";
text += " there";

This creates a new string value because C# strings are immutable.

Mistake 5: Assuming alias types are less powerful

Some learners think string is a simpler version of String. It is not. You can still call methods on it:

string name = "hello";
Console.WriteLine(name.ToUpper());

It has the full functionality of System.String.

Comparisons

string vs String vs System.String

FormWhat it isSame underlying type?Needs using System;?Common usage
stringC# keyword aliasYesNoPreferred for declarations
String.NET type name in System namespaceYesUsually yesSometimes used for static members
System.StringFully qualified .NET type nameYesNoExplicit or namespace-safe usage

Similar aliases in C#

C# alias.NET type
intSystem.Int32
boolSystem.Boolean
objectSystem.Object
decimalSystem.Decimal
doubleSystem.Double

Which should you prefer?

SituationRecommended form
Variable declarationsstring
Method parameters and return typesstring
Fully explicit type referenceSystem.String
Following a team convention using .NET namesString if required by the style guide
Take Quiz

Cheat Sheet

Quick facts

  • string == System.String
  • String usually means System.String
  • No performance difference
  • No behavior difference
  • string is the C# keyword alias
  • String is the .NET type name

Common syntax

string a = "hello";
String b = "hello";
System.String c = "hello";

Recommendation

  • Use string for declarations in most C# code
  • Be consistent across the project

Notes

  • String may require using System;
  • Strings in C# are immutable
  • Static methods can be called like this:
string.IsNullOrEmpty(value)
String.IsNullOrEmpty(value)
System.String.IsNullOrEmpty(value)

Remember

If you know that int means System.Int32, then string meaning System.String follows the same rule.

FAQ

Is string different from String in C#?

No. string is the C# alias for System.String, so they represent the same type.

Which is better to use in C#: string or String?

Most C# developers prefer string for variable and method declarations because it matches other built-in aliases like int and bool.

Is there any performance difference between string and String?

No. They compile to the same underlying .NET type.

Why does C# have both string and String?

C# includes keyword aliases for common .NET types to make code easier to read and write.

Do I need using System; to use String?

Usually yes, unless you write the full name as System.String.

Can I call methods on a string variable?

Yes. A string variable is a System.String object, so you can call methods like ToUpper(), Trim(), and Contains().

Should I use System.String directly?

Usually not for everyday code. It is mainly useful when you want to be fully explicit or avoid namespace ambiguity.

Are C# strings mutable?

No. Strings are immutable, which means operations like concatenation create new string values.

Take Quiz

Mini Project

Description

Build a small C# console program that demonstrates that string, String, and System.String all refer to the same type. This helps reinforce the idea that the difference is mostly about syntax and style, not behavior.

Goal

Create a program that declares strings in different ways and proves they all have the same runtime type.

Requirements

  • Declare one variable using string
  • Declare one variable using String
  • Declare one variable using System.String
  • Print the runtime type of each variable
  • Show whether the types are equal
Take Quiz

Keep learning

Related questions

View more