Open sandboxFocus

Class UnsafeString

A generalized representation of a string that can be backed by a system string, a char[], or an UnsafeStringBuilder. Conversions between these types happen transparently and are cached.

Inheritance
UnsafeString
Namespace: PostSharp.Patterns.Formatters
Assembly: PostSharp.Patterns.Common.dll
Syntax
public sealed class UnsafeString
Remarks

Because an UnsafeString can be backed by an UnsafeStringBuilder, which is a mutable type, and is generally pooled and reused for different purposes, it is generally not safe to evaluate an UnsafeString at a different moment than the one designated by the API that exposes the UnsafeString. To make it safe to evaluate the UnsafeString at any moment, call the MakeImmutable() method, which unbinds the UnsafeString from its parent UnsafeStringBuilder.

Constructors

Name Description
UnsafeString(char[])

Initializes a new instance of the UnsafeString class backed by an array of char.

UnsafeString(string)

Initializes a new instance of the UnsafeString class backed by a string.

Properties

Name Description
Buffer

Gets an unmanaged pointer to the string.

IsImmutable

Gets a value indicating whether the current UnsafeString is immutable or, when the value of this property is false, if it is still bound to a mutable UnsafeStringBuilder. Call the MakeImmutable() method to make the UnsafeString immutable.

Length

Gets the number of characters in the string.

Methods

Name Description
MakeImmutable()

If the current UnsafeString is bound to its origin UnsafeStringBuilder, this method evaluates the UnsafeStringBuilder and breaks the binding, so that later changes in the UnsafeStringBuilder do not cause changes in the current UnsafeString. This method also prevents the UnsafeString from being recycled.

ToCharArray()

Gets an ArraySegment<T> that represents the current UnsafeString.

ToString()