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->System.Stringobject->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:
stringwhen declaring variables, parameters, return types, and fieldsStringonly when calling static members directly, such asString.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, andobject
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()
{
string name = "Mina";
if (string.IsNullOrEmpty(name))
{
Console.WriteLine("Name is empty");
}
else
{
Console.WriteLine(name);
}
}
}
Explanation
string namedeclares a text variablestring.IsNullOrEmpty(name)calls a static helper on the same string type- even though
stringlooks like a keyword, it still maps toSystem.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
-
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. - This is
System.String.
- Returns the runtime type of
-
second.GetType()- Returns the runtime type of
second. - This is also
System.String.
- Returns the runtime type of
-
==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.
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.");
}
// 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
| 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 | No | Explicit or namespace-safe usage |
Similar aliases in C#
| C# alias | .NET type |
|---|---|
int | System.Int32 |
bool | System.Boolean |
object | System.Object |
decimal | System.Decimal |
double | System.Double |
Which should you prefer?
| Situation | Recommended form |
|---|---|
| Variable declarations | string |
| Method parameters and return types | string |
| Fully explicit type reference | System.String |
| Following a team convention using .NET names | String if required by the style guide |
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)
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.
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.
Best Way to Repeat a Character in C#: Building Repeated Strings Efficiently
Learn the best way to repeat a character in C#, compare StringBuilder, string concatenation, and simpler built-in options.
C# Access Modifiers and static: public, private, protected, and Defaults
Learn how C# public, private, protected, and default access control visibility, and how static differs from instance members.