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:
stringis a C# keywordStringusually refers toSystem.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.Int32bool->System.Booleanstring->
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
stringis the short C# labelSystem.Stringis the full official label
They do not represent different boxes. They both point to the same thing.
A similar idea exists in everyday language:
TVandtelevisionmean 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.
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()
{
name = ;
(.IsNullOrEmpty(name))
{
Console.WriteLine();
}
{
Console.WriteLine(name);
}
}
}
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
-
string first = "Hello";- A variable named
firstis created. - Its type is
System.Stringthrough thestringalias.
- A variable named
-
String second = "World";- A variable named
secondis created. - Because
Stringrefers toSystem.String, it has the same type.
- A variable named
-
first.GetType()- Returns the runtime type of
first.
- Returns the runtime type of
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.
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:
stringintboolobject
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.");
}
}
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";
Comparisons
string vs String vs System.String
| Form | What it is | Same underlying type? | Needs using System;? | Common usage |
|---|---|---|---|---|
string | C# keyword alias | Yes | No | Preferred for declarations |
String | .NET type name in System namespace | Yes | Usually yes | Sometimes used for static members |
System.String | Fully qualified .NET type name | Yes |
Cheat Sheet
Quick facts
string==System.StringStringusually meansSystem.String- No performance difference
- No behavior difference
stringis the C# keyword aliasStringis the .NET type name
Common syntax
string a = "hello";
String b = "hello";
System.String c = "hello";
Recommendation
- Use
stringfor declarations in most C# code - Be consistent across the project
Notes
Stringmay requireusing System;- Strings in C# are immutable
- Static methods can be called like this:
string.IsNullOrEmpty(value)
String.IsNullOrEmpty(value)
System.String.IsNullOrEmpty(value)
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 variable?
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
Keep learning
Related questions
AddTransient vs AddScoped vs AddSingleton in ASP.NET Core Dependency Injection
Learn the differences between AddTransient, AddScoped, and AddSingleton in ASP.NET Core DI with examples and practical usage.
C# Type Checking Explained: typeof vs GetType() vs is
Learn when to use typeof, GetType(), and is in C#. Understand exact type checks, inheritance, and safe type testing clearly.
C# Version Numbers Explained: C# vs .NET Framework and Why “C# 3.5” Is Incorrect
Learn the correct C# version numbers, how they map to .NET releases, and why terms like C# 3.5 are inaccurate and confusing.