In C#, string.Create() is an advanced way to build a new string. You say how long it will be, and .NET gives you a writable Span<char> (a window onto the new string's characters) inside a small function. You fill the characters, and when your function returns, the finished string is handed back.
The benefit is speed: the characters are written straight into the final string, with no temporary arrays or StringBuilder. For everyday code, Concat(), interpolation, or Join() are simpler. Reach for Create when you build many strings in a hot loop. Newer .NET also has a friendly overload that formats an interpolated string with a chosen culture.
This is a static member of System.String. Call it on the type, such as string.Create(...), not on a string variable. The Create(length, state, action) form exists in .NET Core 2.1 and later (including .NET 5+). The Create(provider, $"...") interpolation form is newer: .NET 6 and later. Neither exists on .NET Framework. Strings stay immutable: the characters are written only while the new string is being built, before your code ever sees it.
Syntax
Pass the length, a state value your function needs, and the function that fills the span.
C#
string stars = string.Create(5, '*', (span, ch) => {
for (int i = 0; i < span.Length; i++) {
span[i] = ch;
}
}); // "*****"
// .NET 6+: format with a chosen culture
string total = string.Create(CultureInfo.InvariantCulture, $"Total: {1234.5:N2}"); // "Total: 1,234.50"
Call
What it does
string.Create(len, state, (span, s) => { ... })
Make a string of len chars; your function fills span using s.
Format an interpolated string with fixed culture rules (.NET 6+).
Inside the function, write every position of the span; the string must be completely filled. Do not keep the span after the function returns. Pass what you need through state rather than using outside variables; then the lambda can be static and no hidden objects are created.
Library declarations
C#
public static string Create<TState>(int length, TState state, SpanAction<char, TState> action);
public static string Create(IFormatProvider? provider, ref DefaultInterpolatedStringHandler handler); // .NET 6+
public static string Create(IFormatProvider? provider, Span<char> initialBuffer, ref DefaultInterpolatedStringHandler handler); // .NET 6+
// System.Buffers
public delegate void SpanAction<T, in TArg>(Span<T> span, TArg arg);
Parameters
Parameter
Description
length
Required. How many characters the new string will have. 0 returns ""; negative throws.
state
Required. Any value your function needs, passed in as its second argument.
action
Required. The function (span, state) => { ... } that writes the characters.
provider
For the interpolation overload: the culture used to format numbers and dates.
Return Value
Return Value
Description
string
The new string, exactly length characters long, holding what your function wrote.
C# Examples
Example 1 — Fill a New String with One Character
Build a row of stars by writing each position of the span.
C#
using System;
class Program {
static void Main() {
string stars = string.Create(5, '*', (span, ch) => {
for (int i = 0; i < span.Length; i++) {
span[i] = ch;
}
});
Console.WriteLine(stars);
Console.WriteLine(stars.Length);
}
}
Output
*****
5
How It Works
Length first. The 5 tells .NET to reserve a string of exactly five characters.
State in, as an argument. The '*' is passed in as ch, so the lambda does not reach outside for it.
Write every slot. The loop sets positions 0 to 4. span.Length is always the length you asked for.
Simpler for this case.new string('*', 5) gives the same text; this example only shows the shape of Create.
Example 2 — Mask a Card Number in One Pass
Copy the last four digits and replace the rest with stars, writing directly into the new string.
C#
using System;
class Program {
static void Main() {
string card = "1234567812345678";
string masked = string.Create(card.Length, card, (span, c) => {
for (int i = 0; i < span.Length; i++) {
span[i] = i < c.Length - 4 ? '*' : c[i];
}
});
Console.WriteLine(masked);
}
}
Output
************5678
How It Works
Same length as the input. The masked text has as many characters as the card number: 16.
The original as state.card goes in as c, so the lambda reads digits from it without capturing a variable.
A decision per position. The first 12 positions get '*'; the last 4 copy the real digits.
No temporary strings. A version built from new string('*', 12) + card.Substring(12) would create two extra strings first.
Example 3 — Format with a Fixed Culture (.NET 6+)
Use the newer overload to format a price and a date with the invariant culture, so the output never depends on the computer.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
double price = 1234.5;
DateTime day = new DateTime(2026, 11, 18);
string a = string.Create(CultureInfo.InvariantCulture, $"Total: {price:N2}");
string b = string.Create(CultureInfo.InvariantCulture, $"Date: {day:yyyy-MM-dd}");
Console.WriteLine(a);
Console.WriteLine(b);
}
}
Output
Total: 1,234.50
Date: 2026-11-18
How It Works
A different overload. The first argument is a culture and the second is an interpolated string. This form appeared in .NET 6.
Invariant number format.N2 means “group thousands and show two decimals”. Invariant rules use a comma for groups and a dot for decimals.
A fixed date pattern.yyyy-MM-dd prints the year, month, and day with dashes, whatever the local date style is.
Why not plain $"..."? A plain interpolated string uses the current culture, so a German computer would print 1.234,50. Passing the culture removes that surprise.
Frequently Asked Questions
It creates a new string of a given length and lets your function write its characters directly through a Span<char>. It avoids temporary buffers, so it is useful in performance-critical code.
Rarely. Use concatenation, interpolation, string.Join, or new string(c, n) first. string.Create is for hot paths where every allocation matters.
It is any value you pass in, which your function receives as its second argument. Passing data through state lets the lambda be static and avoids hidden allocations.
Create(length, state, action) is in .NET Core 2.1 and later. Create(IFormatProvider, interpolated string) is in .NET 6 and later. Neither is in .NET Framework.
No. You can write only while the string is being created, inside your function. After Create returns, the string is complete and can no longer be changed.