System.Security.SecureString.RemoveAt C# (CSharp) Method

RemoveAt() public method

public RemoveAt ( int index ) : void
index int
return void
		public void RemoveAt (int index)
		{
			if (disposed)
				throw new ObjectDisposedException ("SecureString");
			if (read_only) {
				throw new InvalidOperationException (Locale.GetText (
					"SecureString is read-only."));
			}
			if ((index < 0) || (index >= length))
				throw new ArgumentOutOfRangeException ("index", "< 0 || > length");

			try {
				Decrypt ();
				Buffer.BlockCopy (data, index + 1, data, index, data.Length - index - 1);
				Alloc (--length, true);
			}
			finally {
				Encrypt ();
			}
		}

Usage Example

        public static SecureString GetSecureStringFromConsole()
        {
            var password = new SecureString();

            Console.Write("Enter Password: ");
            while (true)
            {
                ConsoleKeyInfo cki = Console.ReadKey(true);

                if (cki.Key == ConsoleKey.Enter) break;
                if (cki.Key == ConsoleKey.Escape)
                {
                    password.Dispose();
                    return null;
                }
                if (cki.Key == ConsoleKey.Backspace)
                {
                    if (password.Length != 0)
                        password.RemoveAt(password.Length - 1);
                }
                else password.AppendChar(cki.KeyChar);
            }

            return password;
        }
All Usage Examples Of System.Security.SecureString::RemoveAt