Leer en inglés

Compartir a través de


SecureString.SetAt(Int32, Char) Método

Definición

Reemplaza con otro carácter el carácter existente en la posición de índice especificada.

C#
public void SetAt(int index, char c);
C#
[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]
public void SetAt(int index, char c);

Parámetros

index
Int32

Posición de índice de un carácter existente en esta cadena segura.

c
Char

Carácter que reemplaza al carácter existente.

Atributos

Excepciones

Esta cadena segura ya se ha desechado.

Esta cadena segura es de sólo lectura.

El parámetro index es menor que cero o mayor o igual que la longitud de la cadena segura en cuestión.

Error al proteger o desproteger el valor de esta cadena segura.

Ejemplos

En el ejemplo siguiente se muestra cómo los AppendCharmétodos , InsertAtRemoveAt, SetAt, y Clear afectan al valor de un SecureString objeto .

C#
using System;
using System.Security;

class SecureStringExample
{
    public static void Main()
    {
       string msg = "The current length of the SecureString object: {0}\n";
       Console.WriteLine("1) Instantiate the SecureString object.");
       SecureString ss = new SecureString();
       Console.WriteLine(msg, ss.Length);

       Console.WriteLine("2) Append 'a' to the value.");
       ss.AppendChar('a');
       Console.WriteLine(msg, ss.Length);

       Console.WriteLine("3) Append 'X' to the value.");
       ss.AppendChar('X');
       Console.WriteLine(msg, ss.Length);

       Console.WriteLine("4) Append 'c' to the value.");
       ss.AppendChar('c');
       Console.WriteLine(msg, ss.Length);

       Console.WriteLine("5) Insert 'd' at the end of the value.");
       ss.InsertAt(ss.Length, 'd');
       Console.WriteLine(msg, ss.Length);

       Console.WriteLine("6) Remove the last character ('d') from the value.");
       ss.RemoveAt(3);
       Console.WriteLine(msg, ss.Length);

       Console.WriteLine("7) Set the second character of the value to 'b'.");
       ss.SetAt(1, 'b');
       Console.WriteLine(msg, ss.Length);

       Console.WriteLine("8) Delete the value of the SecureString object:");
       ss.Clear();
       Console.WriteLine(msg, ss.Length);

       ss.Dispose();
    }
}
// The example displays the following output:
//       1) Instantiate the SecureString object.
//       The current length of the SecureString object: 0
//
//       2) Append 'a' to the value.
//       The current length of the SecureString object: 1
//
//       3) Append 'X' to the value.
//       The current length of the SecureString object: 2
//
//       4) Append 'c' to the value.
//       The current length of the SecureString object: 3
//
//       5) Insert 'd' at the end of the value.
//       The current length of the SecureString object: 4
//
//       6) Remove the last character ('d') from the value.
//       The current length of the SecureString object: 3
//
//       7) Set the second character of the value to 'b'.
//       The current length of the SecureString object: 3
//
//       8) Delete the value of the SecureString object:
//       The current length of the SecureString object: 0

Comentarios

El índice es de base cero; el primer carácter de esta instancia está en la posición de índice cero.

Si la implementación usa un mecanismo de protección, como el cifrado, el valor de la cadena segura, si existe, no está protegido; c se asigna a la posición de índice especificada; a continuación, se vuelve a proteger el nuevo valor.

Se aplica a

Producto Versiones
.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, 10
.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