Working with Tuples in C#

Tuples in C#




Author - Jp Lazaro  

What is Tuple?

 Is a data structure, that allows you to group a fixed set of elements of different data types in single container or unit.  

Key Features

  •  Grouping elements or types in a single unit 
    •  The primary purpose of Tuple is to group types in single unit for faster and efficient way of processing
  • Multiple / Different types
    • Processing different types in your functions/method is quite difficult, using Tuples it enables us to use multiple or different types at once. This helps processing much easier and processing data and returning it all in a single unit.
  • Performance
    • Compare to class , using tuple is quite fast than class when grouping data is concern

Initializing a tuple

To initiate a tuple in C#, follow the syntax below and example  

Syntax

var variableName = new Tuple<T1, T2, ..., TN>(v1,v2,..,v3);

Example

var person = new Tuple<string, int, string>("John Laz", 40, "Singapore");



Accessing Tuple

In order to process a data for your specific use case. You have to access your tuple and get the values of it. To access the value of tuple you have to follow the below syntax. You can use the .Item1 , .Item2 where n is the index of the data.

    Syntax

    var getTupleData_data1 = variableName.Item1;

    var getTupleData_data2 = variableName.Item2;

    var getTupleData_data3 = variableName.Item3;


    Example

    var person = new Tuple<string, int, string>("John Laz", 40, "Singapore");

    var getName = person.Item1;

    var getAge = person.Item2;

    var getCountry = person.Item3;

    In C# 7 and above

    you can use the below syntax to initialize a tuple. As you can see
    in the example below C# is able to understand that you are assigning a tuple to the person variable
    without telling C#  that the data is tuple

    Syntax

    var variableName = (v1,v2,..,v3);

    Example

    var person = ("John Laz", 40, "Singapore");

    var getName = person.Item1;

    Another example is to define an argument in tuple. By defining an argument, you can specify the name of tuple data you are defining. This is a very good feature, so you can see what kind of data you are parsing and processing to. See example below

    Syntax 
    where  t = data type , n= type name - example "age", "country" and V= value of data type

    (t1 n1, t1 n2, tn nn) variableName = (v1,v2...,vn);

    Example

    (string name, int age, string country) person = ("John",40, "Singapore");

    var getName = person.name;

    Compare to using .ItemN or .Item1..2..3 it is much better to use .propertyname or person.name in the example above to clearly know what data you are processing.



    Usage

    Below are the common use cases that you can apply tuples

    • Passing multiple arguments to a function
    • Processing multiple arguments to a function
    • Returning multiple values from a function
    • Representing data structure for multiple data types
    • Representing temporary data structures


    For more questions you can leave comments below
    You can also follow me on youtube 

    https://www.youtube.com/@codewithsirjp

    Comments