Tuple in C#

A Tuple is great way to pass multiple values from a method, for example…

C#
private Tuple<string, int, bool> getStatus()
{
  string element1 = "";
  string element2 = "";
  bool isActive = false;
  
  ... // Some processing to populate the variables
  
  return Tuple.Create(element1, element2, isActive);
{

The returned Tuple can be captured and its contents used as below;

C#
Tuple<string, string, bool> returnedData = getStatus();

if (returnedData.Item3) // display message boxes if true
{
  MessageBox.Show(returnedData.item1);
  MessageBox.Show(returnedData.item2);
}

This very simple example shows how to return a Tuple containing three values, but the elements of a Tuple are not just limited to strings, int’s and booleans. The opportunities are there to get as creative as you need.

There is a limit, however, of up to 8 Tuples within a Tuple (an octuple). The Microsoft reference page for further reading is here: Tuple Class (System)