SecureString Konstruktory

Definice

Inicializuje novou instanci SecureString třídy.

Přetížení

Name Description
SecureString()

Inicializuje novou instanci SecureString třídy.

SecureString(Char*, Int32)

Inicializuje novou instanci SecureString třídy z podarray Char objektů.

Tento konstruktor není kompatibilní se specifikací CLS. Alternativou kompatibilní se specifikací CLS je SecureString().

SecureString()

Zdroj:
SecureString.cs
Zdroj:
SecureString.cs
Zdroj:
SecureString.cs
Zdroj:
SecureString.cs
Zdroj:
SecureString.cs

Inicializuje novou instanci SecureString třídy.

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

Výjimky

Při ochraně nebo zrušení ochrany hodnoty této instance došlo k chybě.

Tato operace není na této platformě podporovaná.

Příklady

Následující příklad používá výchozí konstruktor (nebo bez parametrů) k vytvoření instance nového SecureString objektu. Potom zavolá metodu AppendChar , která do ní přidá pole znaků.

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.

Následující příklad vytvoří SecureString objekt z hodnoty objektu 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.

Platí pro

SecureString(Char*, Int32)

Zdroj:
SecureString.cs
Zdroj:
SecureString.cs
Zdroj:
SecureString.cs
Zdroj:
SecureString.cs
Zdroj:
SecureString.cs

Důležité

Toto rozhraní API neodpovídá specifikaci CLS.

Inicializuje novou instanci SecureString třídy z podarray Char objektů.

Tento konstruktor není kompatibilní se specifikací CLS. Alternativou kompatibilní se specifikací CLS je 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

Parametry

value
Char*

Ukazatel na pole Char objektů.

length
Int32

Počet prvků, které value mají být zahrnuty do nové instance.

Atributy

Výjimky

value je null.

length je menší než nula nebo větší než 65 536.

Při ochraně nebo zrušení ochrany hodnoty tohoto zabezpečeného řetězce došlo k chybě.

Tato operace není na této platformě podporovaná.

Příklady

Následující příklad vytvoří instanci nového SecureString objektu předáním jeho konstruktoru ukazatel na pole znaků.

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.

Poznámky

Tento konstruktor inicializuje nový SecureString objekt na počet znaků zadaných valuelengthpomocí ; hodnota instance je poté zašifrována.

V jazyce C# je tento konstruktor definován pouze v kontextu nebezpečného kódu.

Platí pro