SecureString Konstruktorer
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Initierar en ny instans av SecureString klassen.
Överlagringar
| Name | Description |
|---|---|
| SecureString() |
Initierar en ny instans av SecureString klassen. |
| SecureString(Char*, Int32) |
Initierar en ny instans av SecureString klassen från en underordning av Char objekt. Den här konstruktorn är inte CLS-kompatibel. Det CLS-kompatibla alternativet är SecureString(). |
SecureString()
- Källa:
- SecureString.cs
- Källa:
- SecureString.cs
- Källa:
- SecureString.cs
- Källa:
- SecureString.cs
- Källa:
- SecureString.cs
Initierar en ny instans av SecureString klassen.
public:
SecureString();
public SecureString();
Public Sub New ()
Undantag
Ett fel uppstod när värdet för den här instansen skulle skyddas eller inte skyddas.
Den här åtgärden stöds inte på den här plattformen.
Exempel
I följande exempel används standardkonstruktorn (eller parameterlös) för att instansiera ett nytt SecureString objekt. Den anropar AppendChar sedan metoden för att lägga till en matris med tecken i den.
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.
I följande exempel skapas ett SecureString objekt från värdet för ett String objekt.
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.
Gäller för
SecureString(Char*, Int32)
- Källa:
- SecureString.cs
- Källa:
- SecureString.cs
- Källa:
- SecureString.cs
- Källa:
- SecureString.cs
- Källa:
- SecureString.cs
Viktigt!
Detta API uppfyller inte CLS.
Initierar en ny instans av SecureString klassen från en underordning av Char objekt.
Den här konstruktorn är inte CLS-kompatibel. Det CLS-kompatibla alternativet är 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
Parametrar
- length
- Int32
Antalet element value som ska inkluderas i den nya instansen.
- Attribut
Undantag
value är null.
length är mindre än noll eller större än 65 536.
Ett fel uppstod när värdet för den här säkra strängen skulle skyddas eller avskyddas.
Den här åtgärden stöds inte på den här plattformen.
Exempel
I följande exempel instansierar ett nytt SecureString objekt genom att skicka konstruktorn en pekare till en teckenmatris.
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.
Kommentarer
Den här konstruktorn initierar det nya SecureString objektet till det antal tecken som anges av valuelength. Värdet för instansen krypteras sedan.
I C# definieras den här konstruktorn endast i kontexten för osäker kod.