Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how is a tuple different from a class?

Tags:

c#

class

tuples

how is a tuple different from a class? instead of the following code, we can make a class with 3 fields and make objects from it. How is this Tuple different from that? Is it only reducing the code we write or does it have something to do with speed as well, given the fact that you can't change the items in a tuple.

Tuple<int, string, bool> tuple = new Tuple<int, string, bool>(1, "cat", true);
like image 636
nasim Avatar asked Dec 18 '14 23:12

nasim


2 Answers

It saves you from having to define a new class with custom properties.

It does define equality by the value of the three items, which is something that a bare-bones class would not do without custom coding. That plus the fact that it's immutable makes it a reasonable candidate for a hash key in a Dictionary.

One drawback is that the properties are vanilla Item1, Item2, etc., so they don't provide any context to the values within them, where properties like ID, Name, Age would.

like image 117
D Stanley Avatar answered Sep 30 '22 09:09

D Stanley


Tuple is a class. One that holds any data you want (in terribly named properties like Item1).

You should be making classes instead so your code is more readable/maintainable. Its primary function is as a "quick fix" when you want to associate pieces of data without making a class to hold them.

like image 29
BradleyDotNET Avatar answered Sep 30 '22 11:09

BradleyDotNET