Tuple Class – C#

Tuple class

With tuple class (available from version .Net 4.0) our function can return more then one value. I’ll show you on a simple example.

Imagine that we want to return cars with brand and model but the type of car is common to all these cars.

Let’s see, we’ve got three cars – Audi A4, Mitsubishi Lancer and Mazda RX-8. All of these is passenger car. Instead of add additional propertion to our DTO class represent cars. We can create it as follows.

Class represent Brand.

And class Cars, which returns cars including their type.

Now we can display all of these cars.

As you can see, we specify the parameters in the order in our case item1 is the car type and item2 is the class representing the brand. To appeal to a type car we don’t need to iterate through the collection.



The class tuple should apply only in some specific situations.Of course you will decide when and if, you will use this at all. In addition, this class is immutable.


Link to the project.

Leave a Reply

Your email address will not be published. Required fields are marked *