Skip to content

MultiSelect ​

The MultiSelect prompt lets the user choose multiple items from a list using checkboxes.

Basic Usage ​

csharp
var cities = Prompt.MultiSelect("Which cities would you like to visit?",
    new[] { "Seattle", "London", "Tokyo", "New York", "Singapore", "Shanghai" },
    pageSize: 3);
Console.WriteLine($"You picked {string.Join(", ", cities)}");

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)
minimumintMinimum number of items that must be selected (default: 1)
maximumintMaximum number of items that can be selected (default: unlimited)
defaultValuesIEnumerable<T>?Items selected by default
textSelectorFunc<T, string>?Function to convert items to display text

Options Class ​

csharp
var cities = Prompt.MultiSelect(new MultiSelectOptions<string>
{
    Message = "Which cities would you like to visit?",
    Items = new[] { "Seattle", "London", "Tokyo", "New York", "Singapore", "Shanghai" },
    PageSize = 3,
    Minimum = 1,
    Maximum = 3,
    DefaultValues = new[] { "Tokyo" }
});

Properties ​

PropertyTypeDefaultDescription
ItemsIEnumerable<T>—Items to select from
DefaultValuesIEnumerable<T>[]Items selected by default
PageSizeintint.MaxValueNumber of items per page
Minimumint1Minimum number of selections required
Maximumintint.MaxValueMaximum number of selections allowed
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

Selection 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.
  • DefaultValues that are not in Items are ignored.
  • The result contains the selected items in the order of Items.
  • Minimum and Maximum limit the number of selected items:
OperationResult
Space on an unchecked item at MaximumThe item is not checked, and an error is shown.
Ctrl+A or Ctrl+I that would exceed MaximumNothing changes, and an error is shown.
More DefaultValues than MaximumThey are all checked. Enter is rejected until enough are unchecked.
Enter below Minimum or above MaximumRejected with an error.

Ctrl+A and Ctrl+I only affect the items that match the current filter. Checked items hidden by the filter stay checked.

Before the prompt is shown, ArgumentOutOfRangeException is thrown when Minimum is negative, when Maximum is less than Minimum, or when Minimum is greater than the number of items, because the prompt could not be answered. For an empty Items, set Minimum to 0.

With Enum Type ​

csharp
var values = Prompt.MultiSelect<MyEnum>("Select enum values", defaultValues: new[] { MyEnum.Bar });
Console.WriteLine($"You picked {string.Join(", ", values)}");

Items are ordered the same way as for Select. See Item Order.

Fluent API ​

csharp
using Sharprompt.Fluent;

var cities = Prompt.MultiSelect<string>(o => o.WithMessage("Which cities would you like to visit?")
                                               .WithItems(new[] { "Seattle", "London", "Tokyo" })
                                               .WithPageSize(3));

Released under the MIT License.