Skip to content

Select ​

The Select prompt lets the user choose a single item from a list.

Basic Usage ​

csharp
var city = Prompt.Select("Select your city", new[] { "Seattle", "London", "Tokyo" });
Console.WriteLine($"Hello, {city}!");

Parameters ​

ParameterTypeDescription
messagestringThe message to display to the user
itemsIEnumerable<T>?Items to select from (auto-generated for enum types)
pageSizeintNumber of items to display per page (default: unlimited)
defaultValueobject?Default selected value
textSelectorFunc<T, string>?Function to convert items to display text

Options Class ​

csharp
var city = Prompt.Select(new SelectOptions<string>
{
    Message = "Select your city",
    Items = new[] { "Seattle", "London", "Tokyo", "New York", "Singapore", "Shanghai" },
    PageSize = 3,
    DefaultValue = "Seattle"
});

Properties ​

PropertyTypeDefaultDescription
ItemsIEnumerable<T>—Items to select from
DefaultValueobject?nullDefault selected value
PageSizeintint.MaxValueNumber of items per page
TextSelectorFunc<T, string>x => x.ToString()!Display text selector
LoopingSelectionbooltrueWhether Up/Down wraps within the current page (true) or moves between pages (false); page navigation still wraps between the first and last pages

With Pagination ​

csharp
var city = Prompt.Select("Select your city",
    new[] { "Seattle", "London", "Tokyo", "New York", "Singapore", "Shanghai" },
    pageSize: 3);
Console.WriteLine($"Hello, {city}!");

Item Rules ​

  • Items is read once, when the prompt starts. Equal items (compared with EqualityComparer<T>.Default) are listed once, at the position of the first one.
  • DefaultValue selects the matching item when the prompt starts. If it is not in Items, it does not select anything. A value of a compatible type, such as an int for a long prompt, is converted; an incompatible one throws ArgumentException.
  • Enter with nothing selected shows an error. When only one item is listed, with or without a filter, or when the filter exactly matches an item's text, that item is selected.
  • An empty Items throws ArgumentException before the prompt is shown, because nothing could be selected.

With Enum Type ​

When using an enum type, items are automatically generated:

csharp
var value = Prompt.Select<MyEnum>("Select enum value");
Console.WriteLine($"You selected {value}");

Items are ordered by value, with [Display(Order)] members first. See Item Order for the exact rules, and Enum Support for more details.

Fluent API ​

csharp
using Sharprompt.Fluent;

var city = Prompt.Select<string>(o => o.WithMessage("Select your city")
                                       .WithItems(new[] { "Seattle", "London", "Tokyo" })
                                       .WithDefaultValue("Seattle"));

Released under the MIT License.