Edit

Share via


String.Create Method

Definition

Overloads

Create(IFormatProvider, Span<Char>, DefaultInterpolatedStringHandler)

Creates a new string by using the specified provider to control the formatting of the specified interpolated string.

Create(IFormatProvider, DefaultInterpolatedStringHandler)

Creates a new string by using the specified provider to control the formatting of the specified interpolated string.

Create<TState>(Int32, TState, SpanAction<Char,TState>)

Creates a new string with a specific length and initializes it after creation by using the specified callback.

Create(IFormatProvider, Span<Char>, DefaultInterpolatedStringHandler)

Source:
String.cs
Source:
String.cs
Source:
String.cs

Creates a new string by using the specified provider to control the formatting of the specified interpolated string.

C#
public static string Create(IFormatProvider? provider, Span<char> initialBuffer, ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler handler);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

initialBuffer
Span<Char>

The initial buffer that may be used as temporary space as part of the formatting operation. The contents of this buffer may be overwritten.

handler
DefaultInterpolatedStringHandler

The interpolated string, passed by reference.

Returns

The string that results for formatting the interpolated string using the specified format provider.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Create(IFormatProvider, DefaultInterpolatedStringHandler)

Source:
String.cs
Source:
String.cs
Source:
String.cs

Creates a new string by using the specified provider to control the formatting of the specified interpolated string.

C#
public static string Create(IFormatProvider? provider, ref System.Runtime.CompilerServices.DefaultInterpolatedStringHandler handler);

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

handler
DefaultInterpolatedStringHandler

The interpolated string, passed by reference.

Returns

The string that results for formatting the interpolated string using the specified format provider.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Create<TState>(Int32, TState, SpanAction<Char,TState>)

Source:
String.cs
Source:
String.cs
Source:
String.cs

Creates a new string with a specific length and initializes it after creation by using the specified callback.

C#
public static string Create<TState>(int length, TState state, System.Buffers.SpanAction<char,TState> action);

Type Parameters

TState

The type of the element to pass to action.

Parameters

length
Int32

The length of the string to create.

state
TState

The element to pass to action.

action
SpanAction<Char,TState>

A callback to initialize the string.

Returns

The created string.

Remarks

The initial content of the destination span passed to action is undefined. Therefore, it is the delegate's responsibility to ensure that every element of the span is assigned. Otherwise, the resulting string could contain random characters.

To support interop scenarios, the underlying buffer is guaranteed to be at least 1 greater than represented by the span parameter of the action callback. This additional index represents the null-terminator and, if written, that is the only value supported. Writing any value other than the null-terminator corrupts the string and is considered undefined behavior.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1