Другие языки программирования и технологии

Как в C# скрыть ввод и вывод данных в консоль?

То есть, может есть какой то метод, для того что бы когда я ввожу текст с клавиатуры, он не отображался в консоли или заменялся символом " * " ?
using System;

namespace q86442370 {
    class Program {
        static void Main(string[] args) {
            Console.Write("password: ");
            string pwd = "";
            while (true) {
                ConsoleKeyInfo i = Console.ReadKey(true);
                if (i.Key == ConsoleKey.Enter) {
                    Console.Write('\n');
                    break;
                } else if (i.Key == ConsoleKey.Backspace) {
                    pwd = pwd.Remove(pwd.Length - 1);
                    Console.Write("\b \b");
                } else {
                    pwd += i.KeyChar;
                    Console.Write("*");
                }
            }
            Console.WriteLine("password: " + pwd);
            Console.ReadKey();
        }
    }
}
Бердымурат Атаджанов
Бердымурат Атаджанов
53 606
Лучший ответ
Немного исправил вышестоящий код, завернул в метод

public static string HideCharacter()
{
string text = "";
ConsoleKeyInfo keyInfo;

while (true)
{
keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Enter)
{
break;
}
else if (keyInfo.Key == ConsoleKey.Backspace)
{
if (text.Length == 0)
continue;
text = text.Remove(text.Length - 1);
Console.Write("\b \b");
}
else
{
text += keyInfo.KeyChar;
Console.Write("*");
}
}

return text;
}