Skip to footer content
Iron Academy Logo
C# Application
C# Application

Other Categories

Requesting Data from the User - Spectre Console Series

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.

Introduction: Command Line Input Made Easier

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.

Reading Integer Values with Ask

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);
}
static void Main(string[] args)
{
    Console.Write("Enter integer age: ");
    string input = Console.ReadLine();
    int age = int.Parse(input);
}

This 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?");
int age = AnsiConsole.Ask<int>("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.

Boolean Input and Why Prompt is Better

Next (1:14) Tim demonstrates asking for a Boolean:

bool isHappy = AnsiConsole.Ask<bool>("Are you happy?");
bool isHappy = AnsiConsole.Ask<bool>("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 Askhandles data types automatically, it’s not always the most natural for user input.

Spectre Console Requesting Data User 1 related to Boolean Input and Why Prompt is Better

This is where Tim moves to a more flexible API — Prompt — which behaves more like a mini class that you can customize.

Using TextPromptfor Choices

At 3:02 Tim switches from Ask to Prompt with a TextPrompt. 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"));
var happyText = AnsiConsole.Prompt(
    new TextPrompt<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.

Spectre Console Requesting Data User 2 related to Using TextPrompt for Choices

Adding a Default Value

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.

Prompting for Integers with Validation

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(),
            };
        }));
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(),
            };
        }));

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.

Spectre Console Requesting Data User 3 related to Prompting for Integers with Validation

This built-in validation removes the need to manually write if statements in your main function, call parse methods, and handle exceptions.

Ask vs Prompt: When to Use Each

At 9:00 Tim summarizes:

  • Ask: Quick and simple. Perfect when your program just needs to read one input parameter and convert it to a known type.

  • Prompt: More powerful. Lets you add choices, defaults, and validations. Great when you’re building a richer console application or integrating with other programs that call your executable file with command line arguments.

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.

Why It Matters for Console Developers

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:

  • Rich output in color at the command prompt.

  • Automatic parsing of numeric types, double values, or string values.

  • Friendly error handling for invalid input.

  • The ability to combine prompts with traditional command line arguments for hybrid input models.

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.

Conclusion

By following Tim Corey’s walkthrough in the video, you learn:

  • How to use Spectre.Console’s Askto read and convert user input.

  • How to use TextPromptto present choices, defaults, and validation.

  • How these prompts fit naturally into your Main(string[] args) entry point alongside command line arguments.

  • How to reduce boilerplate error checking and make your command line apps friendlier.

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.

Hero Worlddot related to Requesting Data from the User - Spectre Console Series
Hero Affiliate related to Requesting Data from the User - Spectre Console Series

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!