Working with CHOICE

The CHOICE type is represented by a C# class that contains choice members (alternatives) with an additional Id property that enumerates the available alternatives.


   Order  ::= CHOICE { one Lunch, two Dinner } 
   Lunch  ::= SEQUENCE { item UTF8String, fries BOOLEAN } 
   Dinner ::= SEQUENCE { appetizer UTF8String, entry UTF8String, drinks INTEGER } 

Here is the sample code that shows how to use the Order class:

// Create both choices
Proj.MyModule.Lunch myLunch = new Proj.MyModule.Lunch();
myLunch.Item = "Chicken"; 
myLunch.Fries = true;
Proj.MyModule.Dinner myDinner = new Proj.MyModule.Dinner();
myDinner.Entry = "Fish"; 
myDinner.Drinks = 2; 

// Create new Order 
Proj.MyModule.Order myOrder = new Proj.MyModule.Order(); 

// Select lunch  
myOrder.One = myLunch; // *v3  myOrder.One.SetValue(myLunch);

switch (myOrder.Selected) // **v3: use if (myOrder.IsOne())...  
  case Proj.MyModule.Order.Id.OneChosen: 
    Console.WriteLine("Enjoy your lunch."); break; 
  case Proj.MyModule.Order.Id.TwoChosen: 
    Console.WriteLine("Enjoy your dinner."); break; 
  case Proj.MyModule.Order.Id.Unselected: 
    Console.WriteLine("No Order items selected. Nothing to encode.");

NOTE 1: When the data defined in an ASN.1 schema becomes more complex (includes multiple levels, which result in multiple types defined in the generated code, also known as data bindings), the OSS ASN.1/C# Tools version 4 provides a more efficient method of managing objects owned by the application. In version 4, the application explicitly creates instances of all objects, including nested objects, whereas in version 3, the application explicitly creates an instance of the top level object, and the library implicitly creates instances of nested objects.

NOTE 2: Starting with OSS ASN.1/C# Tools version 4, you can use either "switch" or "if/else" with the myOrder.Selected property. In version 3, the Id property is not generated for CHOICE types, so you must use "if/else".

Download files for this article. Note that you must first compile the .asn file to use the generated files with the Program.cs file.


C# | CHOICE | Migration

The samples included with some of the Knowledge Center answers are meant for your general understanding of the OSS products. Different versions of the products might produce slightly different outputs. Consult the products documentation and samples for the most up-to-date products information and code examples.

Contact Support
contact Our office hours
24 hours/day, 7 days/week

  • Phone: 1-888-OSS-2761 (USA and Canada)
  • Phone: 1-732-302-9669 (International)
  • Fax: 1-732-302-0023
  • Email:
Free Trial

Test drive the OSS Nokalva ASN.1, LTE, and XML Tools now! Your trial includes complete software, documentation, sample programs, free 24x7 technical support and more.

Learn ASN.1
Learn ASN.1

Our expert personnel can help you learn ASN.1!

We offer 4-day ASN.1 courses at our headquarters or your premises.