SystemExt/Terminal/Prompt.cs
/// <summary>
/// Prompts the user to enter a textual answer to a question.
/// </summary>
/// <param name="question">
/// The question to ask the user.
/// </param>
/// <param name="defaultValue">
/// The answer to return if the user just presses enter.
/// </param>
/// <param name="validator">
/// A function which validates the user's input.
/// </param>
/// <returns>
/// The response given by the user.
/// </returns>
Prompts the user to enter a textual answer to a question.
Prompts the user to enter a textual answer to a question .
public static string String(string question, string defaultValue = null, Func<string, bool> validator = null)
        {
            while (true)
            {
                Console.WriteLine("{0}?", question);
                Console.Write("[{0}] => ", defaultValue ?? "(null)");
                var input = Console.ReadLine();
                if (string.IsNullOrEmpty(input))
                    return defaultValue;
                if (validator != null && !validator(input))
                {
                    Console.Error.WriteLine("The value you have given is not valid.");
                    continue;
                }
                return input;
            }
        }
public static string String ( string question , string defaultValue = null , Func < string , bool > validator = null ) { while ( true ) { Console . WriteLine ( " " , question ) ; Console . Write ( " " , defaultValue ?? " " ) ; var input = Console . ReadLine ( ) ; if ( string . IsNullOrEmpty ( input ) ) return defaultValue ; if ( validator != null && ! validator ( input ) ) { Console . Error . WriteLine ( " " ) ; continue ; } return input ; } }
Prompts the user to enter a textual answer to a question.
Prompts the user to enter a textual answer to a question .
The response given by the user.
The response given by the user .
question
string
The question to ask the user.
The question to ask the user .
defaultValue
string
The answer to return if the user just presses enter.
The answer to return if the user just presses enter .
validator
Func<string, bool>
A function which validates the user's input.
A function which validates the user ' s input .