This documentation is archived and is not being maintained.

SecureString Constructor (Char*, Int32)

Updated: October 2008

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Initializes a new instance of the SecureString class from a subarray of System.Char objects.

This API is not CLS-compliant. The CLS-compliant alternative is SecureString().

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

[CLSCompliantAttribute(false)]
public SecureString(
	char* value,
	int length
)

Parameters

value
Type: System.Char*

A pointer to an array of System.Char objects.

length
Type: System.Int32

The number of elements of value to include in the new instance.

ExceptionCondition
ArgumentNullException

value is null.

ArgumentOutOfRangeException

length is less than zero or greater than 65536.

CryptographicException

An error occurred while encrypting or decrypting the value of this secure string.

NotSupportedException

This operation is not supported on this platform.

This constructor initializes the new SecureString object to the number of characters in value specified by length; the value of the instance is then encrypted.

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

Windows 2000 Platform Note: SecureString is only supported on Windows 2000 Service Pack 3 and later.

The following example instantiates a new SecureString object by passing its constructor a pointer to a character array.

using System;
using System.Security;

public class Example
{
   unsafe public static void Main()
   {
      SecureString testString;
      // Define the string value to assign to a new secure string. 
      char[] chars = { 't', 'e', 's', 't' };

      // Instantiate a new secure string.
      fixed(char* pChars = chars)
      {
         testString = new SecureString(pChars, chars.Length);
      }
      // Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", 
                        testString.Length);
   }
}
// The example displays the following output: 
//      The length of the string is 4 characters.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

Date

History

Reason

October 2008

Added an example.

Customer feedback.

July 2008

Added comment on constructor and unsafe code.

Content bug fix.

Show: