Net-informations.com

C# Tuple

A tuple is a data structure that has a specific number and sequence of values, the values in a tuple can either be of the same type or even be of dissimilar types . It introduced in .NET Framework 4.0 and the System.Tuple class provides static methods to create tuple objects.


how to use C# tuple

Syntax

example

C# Tuples allow us to Create, access, and manipulate a data set. You can add elements from 1 to 8 . If you try to add elements greater than eight, then the compiler will throw an error. Tuples allows us to represent multiple data into a single data set. It can also store duplicate elements . Moreover, it allows you to pass multiple values to a method with the help of single parameters.

The order of the elements in a Tuple is defined at the time of creation. Once we create the Tuple, we cannot change the values of its fields (immutable) . Also, the size of the Tuple is fixed since it cannot be changed once it has been defined at the time of creation.

Create() method in Tuple

You can create a Tuple < > using its constructor or the "Create()" method. The following code snippet shows how you can create and initialize a Tuple.

Accessing Tuple Elements

You can access the tuple object elements with Item < positionNumber > properties. For example, the first element can be accessed like Item1, the second element can be accessed like Item2, and so on based on our requirements.

output

example

output

How to iterate over a C# tuple?


iterate loop over a C# tuple

Once your Tuple is created and initialized, you can iterate it much the same way you would with a collection .

output

Nested Tuple

You can include the nested tuple object anywhere in the tuple sequence. However, it is recommended to place the nested tuple at the end of the sequence so that you can be accessed using the tuple Rest property .

output

Limitations:

The Tuple classes will cause more performance issues because they are reference types.










net-informations.com (C) 2020    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About