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 to loop at the beginning/end of the list

With Pagination

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

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}");

See 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.