Choices Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Represents a set of alternatives in the constraints of a speech recognition grammar.

Namespace:   System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)


public class Choices


Initializes a new instance of the Choices class that contains an empty set of alternatives.


Initializes a new instance of the Choices class from an array containing one or more GrammarBuilder objects.


Initializes a new instance of the Choices class from an array containing one or more String objects.


Adds an array containing one or more GrammarBuilder objects to the set of alternatives.


Adds an array containing one or more String objects to the set of alternatives.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a GrammarBuilder object from this Choices object.


Returns a string that represents the current object.(Inherited from Object.)

A Choices object represents a component of a phrase that can have one of several values. Use this class when creating a speech recognition grammar from a GrammarBuilder object.

For example, a Choices object could represent the component colorChoice in the phrase, "Change the color to colorChoice", where acceptable values for colorChoice are “red”, or “green”, or “blue”.


To use a Choices object as an optional component in a phrase, create the Choices object and add it to a GrammarBuilder.GrammarBuilder(GrammarBuilder, Int32, Int32) object with minRepeat and maxRepeat set to 0 and 1, respectively. Phrases containing optional components can be recognized whether or not the optional component is spoken.

The Choices class serves the same function as the one-of XML element defined by the Speech Recognition Grammar Specification (SRGS) Version 1.0 and is similar to the SrgsOneOf class in the System.Speech.Recognition.SrgsGrammar namespace.

For more information about defining a speech recognition grammar, see Speech Recognition and Creating GrammarBuilder Grammars.

The following example creates a speech recognition grammar for the phrase, "Set background to colorChoice", where colorChoice can be one of the defined colors. The GrammarBuilder is used to define the constraints for the grammar.

private Grammar CreateColorGrammar()

  // Create a Choices object that contains a set of alternative colors.
  Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
  colorChoice.Add(new string[] {"cyan", "yellow", "magenta"});

  // Construct the phrase.
  GrammarBuilder builder = new GrammarBuilder("Set background to");

  // Create a grammar for the phrase.
  Grammar colorGrammar = new Grammar(builder);
  colorGrammar.Name = "SetBackground";

  return colorGrammar;

.NET Framework
Available since 3.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top