SecureString Constructors

Definition

Initializes a new instance of the SecureString class.

Overloads

SecureString()

Initializes a new instance of the SecureString class.

SecureString(Char*, Int32)

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

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

SecureString()

Source:
SecureString.cs
Source:
SecureString.cs
Source:
SecureString.cs

Initializes a new instance of the SecureString class.

public SecureString ();

Exceptions

An error occurred while protecting or unprotecting the value of this instance.

This operation is not supported on this platform.

Examples

The following example uses the default (or parameterless) constructor to instantiate a new SecureString object. It then calls the AppendChar method to add an array of characters to it.

using System;
using System.Security;

public class Example
{
   public static void Main()
   {
      // Define the string value to assign to a new secure string.
      char[] chars = { 't', 'e', 's', 't' };
      // Instantiate the secure string.
      SecureString testString = new SecureString();
      // Assign the character array to the secure string.
      foreach (char ch in chars)
         testString.AppendChar(ch);      
      // Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", 
                        testString.Length);
      testString.Dispose();
   }
}
// The example displays the following output:
//      The length of the string is 4 characters.

The following example creates a SecureString object from the value of a String object.

using System;
using System.Security;

public class Example
{
   public static void Main()
   {
      // Define the string value to be assigned to the secure string.
      string initString = "TestString";
      // Instantiate the secure string.
      SecureString testString = new SecureString();
      // Use the AppendChar method to add each char value to the secure string.
      foreach (char ch in initString)
         testString.AppendChar(ch);
         
      // Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", 
                        testString.Length);
      testString.Dispose();
   }
}
// The example displays the following output:
//      The length of the string is 10 characters.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

SecureString(Char*, Int32)

Source:
SecureString.cs
Source:
SecureString.cs
Source:
SecureString.cs

Important

This API is not CLS-compliant.

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

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

[System.CLSCompliant(false)]
public SecureString (char* value, int length);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public SecureString (char* value, int length);

Parameters

value
Char*

A pointer to an array of Char objects.

length
Int32

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

Attributes

Exceptions

value is null.

length is less than zero or greater than 65,536.

An error occurred while protecting or unprotecting the value of this secure string.

This operation is not supported on this platform.

Examples

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);
      testString.Dispose();
   }
}
// The example displays the following output:
//      The length of the string is 4 characters.

Remarks

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.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1