Есть ли более короткий или более чистый способ написать такой вид проверки ввода + логика поиска? ⇐ C#

Место общения программистов C#
Anonymous
Есть ли более короткий или более чистый способ написать такой вид проверки ввода + логика поиска?

Сообщение Anonymous »

Код: Выделить всё

Console.Write("Enter search code: ");< /code>

string searchCode = Console.ReadLine();< /code>

bool isValid = Regex.IsMatch(searchCode, "^ASD[0-9]{4}[A-Z]$");

Код: Выделить всё

while (!string.IsNullOrEmpty(searchCode) && !isValid)< /code>

{< /code>

    Console.Write("Enter search code: ");< /code>

    searchCode = Console.ReadLine();< /code>

    isValid = Regex.IsMatch(searchCode, "^ASD[0-9]{4}[A-Z]$");< /code>

}

if (!string.IsNullOrEmpty(searchCode))< /code>

{< /code>

var result = List.FirstOrDefault(e => e.code == searchCode);< /code>

if (result == null)< /code>

{< /code>

Console.WriteLine("No match found.");< /code>

}< /code>

else< /code>

{< /code>

Console.WriteLine(< /code>

resul.name< /code>

);< /code>

}< /code>

}< /code>


I’ve done this kind of input-validation and search task a lot in school, so the logic is familiar — but I was wondering if there’s a shorter or more elegant way to do this. It works, but the repeated validation and re-prompting feels a bit verbose. Would love to save time during exams if I could streamline things like this.
I am just interested.

Подробнее здесь: https://stackoverflow.com/questions/796 ... search-log

Вернуться в «C#»