Export (0) Print
Expand All

String Constructor (SByte*)

Initializes a new instance of the String class to the value indicated by a pointer to an array of 8-bit signed integers.

This constructor is not CLS-compliant.  

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

[CLSCompliantAttribute(false)] 
public String (
	sbyte* value
)
J# does not support APIs that consume or return unsafe types.
JScript does not support APIs that consume or return unsafe types.

Parameters

value

A pointer to a null terminated array of 8-bit signed integers.

Exception typeCondition

ArgumentNullException

value is a null reference (Nothing in Visual Basic).

ArgumentException

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

ArgumentOutOfRangeException

The length of the new string to initialize, which is determined by the null termination character of value, is too large to allocate.

AccessViolationException

value specifies an invalid address.

The value parameter is assumed to point to an array representing a string encoded in ASCII; that is, the string is encoded using the ANSI code page.

This constructor processes characters from value starting from the location specified by the pointer until a null character (hexadecimal 0x00) is reached.

If the specified array is not null-terminated, 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 code 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") );
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft