SecureString Konstruktor

Definisi

Menginisialisasi instans baru dari kelas SecureString.

Overload

Nama Deskripsi
SecureString()

Menginisialisasi instans baru dari kelas SecureString.

SecureString(Char*, Int32)

Menginisialisasi instans SecureString baru kelas dari subarray Char objek.

Konstruktor ini tidak sesuai dengan CLS. Alternatif yang mematuhi CLS adalah SecureString().

SecureString()

Sumber:
SecureString.cs
Sumber:
SecureString.cs
Sumber:
SecureString.cs
Sumber:
SecureString.cs
Sumber:
SecureString.cs

Menginisialisasi instans baru dari kelas SecureString.

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

Pengecualian

Terjadi kesalahan saat melindungi atau membuka proteksi nilai instans ini.

Operasi ini tidak didukung pada platform ini.

Contoh

Contoh berikut menggunakan konstruktor default (atau tanpa parameter) untuk membuat instans objek baru SecureString . Kemudian memanggil AppendChar metode untuk menambahkan array karakter ke dalamnya.

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.

Contoh berikut membuat SecureString objek dari nilai String objek.

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.

Berlaku untuk

SecureString(Char*, Int32)

Sumber:
SecureString.cs
Sumber:
SecureString.cs
Sumber:
SecureString.cs
Sumber:
SecureString.cs
Sumber:
SecureString.cs

Penting

API ini bukan kompatibel CLS.

Menginisialisasi instans SecureString baru kelas dari subarray Char objek.

Konstruktor ini tidak sesuai dengan CLS. Alternatif yang mematuhi CLS adalah 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

Parameter

value
Char*

Penunjuk ke array Char objek.

length
Int32

Jumlah elemen yang value akan disertakan dalam instans baru.

Atribut

Pengecualian

value adalah null.

length kurang dari nol atau lebih besar dari 65.536.

Terjadi kesalahan saat melindungi atau membatalkan perlindungan nilai string aman ini.

Operasi ini tidak didukung pada platform ini.

Contoh

Contoh berikut membuat instans objek baru SecureString dengan meneruskan konstruktornya penunjuk ke array karakter.

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.

Keterangan

Konstruktor ini menginisialisasi objek baru SecureString ke jumlah karakter yang ditentukan oleh value; nilai instans kemudian dienkripsilength.

Di C#, konstruktor ini hanya didefinisikan dalam konteks kode yang tidak aman.

Berlaku untuk