Before Tuples, I used to create a class and its variables, then create object from this class and make that object the return type for some functions.
Now, with tuples, I can do the same thing, and in C# 7.0 we can assign understandable names for tuple properties (before this, it was item1
, item2
, etc..)
So now I am wondering, when do I use tuple and when do I create a class in C# 7.0?
Why should we use Tuples? You may want to use a tuple to represent a set of heterogeneous data and provide an easy way to access that data. You can also take advantage of a tuple to return multiple values from a method or even pass multiple values to a method.
The Tuple class does not itself represent a tuple. Instead, it is a class that provides static methods for creating instances of the tuple types that are supported by the . NET Framework.
Tuples provide a third way of returning multiple function values, one that combines aspects of the structure-based and on-the-fly options already mentioned. With tuples, you define the values to be returned as part of the function declaration, a bit like the out-parameter variation.
They are structures (value types) rather than classes (reference types). They are mutable rather than read-only. That is, the value of tuple components can change. Their data members, such as Item1 , Item2 , etc., are fields rather than properties.
As this answer is causing some confusion amongst some folk here, I should clarify that - as per the question - all references to "tuple" here refer to the ValueTuple
type and new tuple syntactic sugar features of C# 7 and in no way refer to the old System.Tuple
reference types.
So now I am wondering, when Should I use tuples and when Should I create a class in c# 7.0?
Only you can really answer that question as it really depends on your code.
However, there are guidelines and rules you can follow in guiding you in choosing between them:
Most of the time, this should not be an issue. However, if you are passing around tuples of large structs, this might have an impact on performance. Ref locals/returns can be used to work around these performance issues, though.
Additionally, because they are values, modifying a copy remotely will not change the original copy. This is a good thing, but could catch some folk out.
The names given to elements are used by the compiler and (in most cases) are not available at run-time. This means that reflection cannot be used to discover their names; they cannot be accessed dynamically and they cannot be used in razor views.
Also this is an important consideration with APIs. A tuple returned from a method is the exception to the rule regarding after-compilation name discoverability. The compiler adds attributes to the method that hold information on the tuple names. This means you can safely return a tuple from a public method in one assembly and access its names in another.
Tuples are much simpler to write than types as they are less verbose and the declaration can be "inlined" (ie declared at the point of use). This works well when declaring a method that returns multiple values, for example.
However, because they are declared at the point of use, if you have MethodA
that calls MethodB
that calls MethodC
and each returns a tuple, you'll need to redefine the tuple at every stage. There isn't (yet) a way of creating an alias of a tuple and re-using it across multiple methods.
For any situation where you might consider using a tuple: simply ask yourself the question: "will a tuple simplify the code here". If the answer is "yes", then use one. And that ultimately is the primary consideration over whether to use a tuple or a custom class.
Generally speaking, named classes have some significance in the design of your system. They are also more verbose to write. For example, you may have a class called MediaFileOpener
. It is important to the design that we know what this class does - we are working with media files!
Anonymous types and tuples are used when there is no design significance and all you want is a lightweight Data Transfer Object (DTO) to move information around.
As rule, if your class requires some documentation to describe what it is for, or if there is behaviour that it provides, use a full class. If all you need is temporary storage or some sort of grouping, use a Tuple. Consider a situation where you want to return more than one value from an async method. Tuple is designed to solve that problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With