String Constructor (SByte*, Int32, Int32)

Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting character position within that array, and a length.

This API is not CLS-compliant. 

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[CLSCompliantAttribute(false)]
public String(
	sbyte* value,
	int startIndex,
	int length
)

Parameters

value
Type: System.SByte*
A pointer to an array of 8-bit signed integers.
startIndex
Type: System.Int32
The starting position within value.
length
Type: System.Int32
The number of characters within value to use.

ExceptionCondition
ArgumentNullException

value is null.

ArgumentOutOfRangeException

startIndex or length is less than zero.

-or-

The address specified by value + startIndex is too large for the current platform; that is, the address calculation overflowed.

-or-

The length of the new string to initialize is too large to allocate.

ArgumentException

The address specified by value + startIndex is less than 64K.

-or-

A new instance of String could not be initialized using value, assuming value is encoded in ANSI.

AccessViolationException

value, startIndex, and length collectively specify an invalid address.

The value parameter is assumed to point to an array representing a string encoded using the default ANSI code page (that is, the encoding method specified by Encoding.Default).

NoteNote

Because the default ANSI code page is system-dependent, the string created by this constructor from identical signed byte arrays may differ on different systems. Instead of using a signed byte array to instantiate a string, the recommended alternative is to use any of the overloads that take either an array of Char (the String.String(Char[]) and String.String(Char[], Int32, Int32) constructors) or a pointer to an array of Char (the String.String(Char*) and String.String(Char*, Int32, Int32) constructors) as parameters.

If length is zero, the new instance is initialized to the empty string ("").

This constructor processes characters from value starting at startIndex and ending at (startIndex + length - 1).

If the specified range is outside of the memory allocated for the sequence of characters, the behavior of this constructor is system dependent. For example, such a situation might cause an access violation.

In C#, this constructor is defined only in the context of unsafe code.

The following simple example demonstrates how you can create an instance of the String class with this constructor.


unsafe
{
    // Null terminated ASCII characters in an sbyte array
    String szAsciiUpper = null;
    sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 };
    // Instruct the Garbage Collector not to move the memory
    fixed(sbyte* pAsciiUpper = sbArr1)
    {
        szAsciiUpper = new String(pAsciiUpper);
    }
    String szAsciiLower = null;
    sbyte[] sbArr2 = { 0x61, 0x62, 0x63, 0x00 };
    // Instruct the Garbage Collector not to move the memory
    fixed(sbyte* pAsciiLower = sbArr2)
    {
        szAsciiLower = new String(pAsciiLower, 0, sbArr2.Length);
    }
    // Prints "ABC abc"
    Console.WriteLine(szAsciiUpper + " " + szAsciiLower);

    // Compare Strings - the result is true
    Console.WriteLine("The Strings are equal when capitalized ? " +
        (String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper())==0?"true":"false") );

    // This is the effective equivalent of another Compare method, which ignores case
    Console.WriteLine("The Strings are equal when capitalized ? " +
        (String.Compare(szAsciiUpper, szAsciiLower, true)==0?"true":"false") );
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

  • SecurityCriticalAttribute 

    requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft