SecureString コンストラクター

定義

SecureString クラスの新しいインスタンスを初期化します。

オーバーロード

名前 説明
SecureString()

SecureString クラスの新しいインスタンスを初期化します。

SecureString(Char*, Int32)

SecureString オブジェクトのサブ配列から、Char クラスの新しいインスタンスを初期化します。

このコンストラクターは CLS に準拠していません。 CLS 準拠の代替手段は SecureString()

SecureString()

ソース:
SecureString.cs
ソース:
SecureString.cs
ソース:
SecureString.cs
ソース:
SecureString.cs
ソース:
SecureString.cs

SecureString クラスの新しいインスタンスを初期化します。

public:
 SecureString();
public SecureString();
Public Sub New ()

例外

このインスタンスの値の保護または保護解除中にエラーが発生しました。

この操作は、このプラットフォームではサポートされていません。

次の例では、既定の (またはパラメーターなしの) コンストラクターを使用して、新しい SecureString オブジェクトをインスタンス化します。 次に、 AppendChar メソッドを呼び出して、文字の配列を追加します。

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.
Imports System.Security

Module Example
   Public Sub Main()
      ' Define the string value to assign to a new secure string.
      Dim chars() As Char = { "t"c, "e"c, "s"c, "t"c }
      ' Instantiate the secure string.
      Dim testString As SecureString = New SecureString()
      ' Assign the character array to the secure string.
      For Each ch As char In chars
         testString.AppendChar(ch)
      Next         
      ' Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", _ 
                        testString.Length)
      testString.Dispose()
   End Sub
End Module
' The example displays the following output:
'      The length of the string is 4 characters.

次の例では、SecureString オブジェクトの値からString オブジェクトを作成します。

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.
Imports System.Security

Module Example
   Public Sub Main()
      ' Define the string value to be assigned to the secure string.
      Dim initString As String = "TestString"
      ' Instantiate the secure string.
      Dim testString As SecureString = New SecureString()
      ' Use the AppendChar method to add each char value to the secure string.
      For Each ch As Char In initString
         testString.AppendChar(ch)
      Next   
      ' Display secure string length.
      Console.WriteLine("The length of the string is {0} characters.", _ 
                        testString.Length)
      testString.Dispose()
   End Sub
End Module
' The example displays the following output:
'      The length of the string is 10 characters.

適用対象

SecureString(Char*, Int32)

ソース:
SecureString.cs
ソース:
SecureString.cs
ソース:
SecureString.cs
ソース:
SecureString.cs
ソース:
SecureString.cs

重要

この API は CLS 準拠ではありません。

SecureString オブジェクトのサブ配列から、Char クラスの新しいインスタンスを初期化します。

このコンストラクターは CLS に準拠していません。 CLS 準拠の代替手段は SecureString()

public:
 SecureString(char* value, int length);
[System.CLSCompliant(false)]
public SecureString(char* value, int length);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public SecureString(char* value, int length);
[<System.CLSCompliant(false)>]
new System.Security.SecureString : nativeptr<char> * int -> System.Security.SecureString
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
new System.Security.SecureString : nativeptr<char> * int -> System.Security.SecureString

パラメーター

value
Char*

Char オブジェクトの配列へのポインター。

length
Int32

新しいインスタンスに含める value の要素の数。

属性

例外

valuenullです。

length が 0 より小さいか、65,536 より大きい値です。

このセキュリティで保護された文字列の値の保護または保護解除中にエラーが発生しました。

この操作は、このプラットフォームではサポートされていません。

次の例では、コンストラクターに文字配列へのポインターを渡すことによって、新しい SecureString オブジェクトをインスタンス化します。

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.

注釈

このコンストラクターは、新しいSecureString オブジェクトを、valueで指定されたlength文字数に初期化します。その後、インスタンスの値が暗号化されます。

C# では、このコンストラクターは安全でないコードのコンテキストでのみ定義されます。

適用対象