Earn More by Sharing What You Love
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Tim Corey
10m 10s
C# console applications have always been the simplest way to learn how a program reads input from the user and displays output. In his Spectre.Console series, Tim Corey shows how to go far beyond the plain black-and-white command prompt. In this article we'll look closely at his video "Requesting Data from the User - Spectre Console Series" to see how he collects input, handles numeric values, does error checking, and gives his program a friendlier face - all without manually modifying boilerplate code. Time stamps are included so you can jump to the right spot in the video.
At the very start Tim reminds us that Spectre.Console can transform ordinary C command line input into something visually appealing. Instead of writing a full static void Main(string[] args) method and then manually reading from the standard input stream with Console.ReadLine(), parsing it, and dealing with exceptions, Spectre.Console wraps that up for you.
In Tim's words (0:17), this session will show how to "ask for input from the user, validate the data, and cast it into the appropriate data type" - a core skill for any developer working with a console or command prompt.
Ask<T>Tim's first example (0:35) is a familiar one: ask the user for their age. Traditionally in C# you would write:
static void Main(string[] args)
{
Console.Write("Enter integer age: ");
string input = Console.ReadLine();
int age = int.Parse(input);
}Sub Main(ByVal args As String())
Console.Write("Enter integer age: ")
Dim input As String = Console.ReadLine()
Dim age As Integer = Integer.Parse(input)
End SubThis shows the classic entry point of a C# program, the main function, and how you receive an input parameter from the keyboard through the console. You also have to use int.Parse or Convert.ToInt32 to convert the string input into an int age. Tim points out that you must handle invalid input and throw or catch exceptions yourself.
With Spectre.Console, he simply writes:
int age = AnsiConsole.Ask<int>("What is your age?");Dim age As Integer = AnsiConsole.Ask(Of Integer)("What is your age?")This one line reads input, converts it to an integer value, and displays a red error message if the user types non-numeric characters - no extra error checking code required. When the user presses Enter, the method reads the input, parses it into a numeric type, and stores it in the age variable.
Next (1:14) Tim demonstrates asking for a Boolean:
bool isHappy = AnsiConsole.Ask<bool>("Are you happy?");Dim isHappy As Boolean = AnsiConsole.Ask(Of Boolean)("Are you happy?")If the user enters "true" or "false," it works. But if they type "yes," Spectre.Console displays a red error. This shows that while Ask<bool> handles data types automatically, it's not always the most natural for user input.

This is where Tim moves to a more flexible API - Prompt - which behaves more like a mini class that you can customize.
TextPrompt<T> for ChoicesAt 3:02 Tim switches from Ask to Prompt with a TextPrompt<string>. Instead of the built-in Boolean, he creates a new instance of a text prompt that gives the user actual choices:
var happyText = AnsiConsole.Prompt(
new TextPrompt<string>("Are you happy?")
.AddChoice("Yes")
.AddChoice("No"));Dim happyText = AnsiConsole.Prompt(
New TextPrompt(Of String)("Are you happy?").
AddChoice("Yes").
AddChoice("No"))When the program executes (4:48), the console shows [Yes/No] after the question. This is Spectre.Console decorating the command line so the user can easily see acceptable answers. If you type anything else, the method receives it, checks it against the list, and displays a friendly message telling you to select an available option.

Tim then adds a default (5:16):
.DefaultValue("Yes").DefaultValue("Yes")Now, when the program runs, the user can simply press Enter - a carriage return with no text - and the method automatically assigns "Yes" to the variable. This is great when you want to store a reasonable default without extra code in your main method.
At 6:01 Tim shows a richer example with integer input. He uses a new TextPrompt to ask for age again but adds validation logic right inside the prompt:
int age = AnsiConsole.Prompt(
new TextPrompt<int>("What is your age?")
.Validate(x =>
{
return x switch
{
< 0 => ValidationResult.Error("[red]You were not born yet[/]"),
> 120 => ValidationResult.Error("[red]You are too old[/]"),
_ => ValidationResult.Success(),
};
}));Dim age As Integer = AnsiConsole.Prompt(
New TextPrompt(Of Integer)("What is your age?").Validate(Function(x)
Select Case x
Case Is < 0
Return ValidationResult.Error("[red]You were not born yet[/]")
Case Is > 120
Return ValidationResult.Error("[red]You are too old[/]")
Case Else
Return ValidationResult.Success()
End Select
End Function))Here the prompt is not only converting the string values from the standard input stream into a numeric type, it's also performing error checking based on business rules - like not allowing negative ages. Tim tests it live (8:31): entering -1 triggers "You were not born yet." Entering 150 triggers "You are too old." Only valid input lets the program continue to the next line.

This built-in validation removes the need to manually write if statements in your main function, call parse methods, and handle exceptions.
At 9:00 Tim summarizes:
Ask<T>: Quick and simple. Perfect when your program just needs to read one input parameter and convert it to a known type.Both approaches still fit neatly into your static void Main(string[] args) or even public static void Main(string[] args) entry point. You can also access args to read command line arguments passed at program execution, combine that with Spectre.Console prompts, and output a neatly formatted string using string.Format or interpolation.
Tim closes (9:19) by noting how this approach "takes your ability to ask for input from the user to the next level." With Spectre.Console you're no longer writing repetitive code to read keyboard input, convert it with the Convert class, and display default messages. Instead you get:
This makes it easier to teach beginners about the main method, data types, and input parameters while still writing professional-grade console applications in Visual Studio or any other editor.
By following Tim Corey's walkthrough in the video, you learn:
Ask<T> to read and convert user input.TextPrompt<T> to present choices, defaults, and validation.Everything Tim shows still compiles into a single executable file that you can run from the command prompt, pass arguments to, or embed in larger Windows Forms applications or tools. It's a modern way to handle input from the user while keeping the simplicity of the C# console and its familiar main function.
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!
Join our newsletter, you’ll get exclusive access on article updates. We value your privacy