Tuple In C# 7.0

C# 7 introduces several great features, including pattern matching, tuples, and local functions. Moreover, several existing features and overall performance have been improved, with an eye towards code simplification and clarity.
 
In this article, I will peruse the new and improved language features and look at ways to take advantage of performance improvements in C# 7 and Visual Studio 2017.
 
I will start by introducing the new features before moving on to the improved things.
 

Tuples

 
Tuples are not completely new in C# 7.0. In .NET Framework 4.0, a set of Tuple classes has been introduced in the System.Tuple namespace.
 
If you want to return more than one value from a method then you need to use Tuples. Besides, in the programming world, it is a very common thing to return multiple values from a method. Tuples in C# 7.0 provides a better mechanism to return multiple values from a method.
 
Old Approach
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         Tuple < stringstring > tuple = GetFullName();  
  4.         Console.WriteLine($ "First Name {tuple.Item1} and Last Name {tuple.Item2}");  
  5.         Console.ReadKey();  
  6.     }  
  7.     private static Tuple < stringstring > GetFullName() {  
  8.         //Creating an object of Tuple class by calling the static Create method  
  9.         Tuple < stringstring > t = Tuple.Create("Prasad""Raveendran");  
  10.         //Returning the tuple instance  
  11.         return t;  
  12.     }  
  13. }  

Drawbacks of this approach

 
Performance
 
Tuples in C# are classes, i.e. reference types. Since it is a reference type, memory is allocated on the heap area and garbage collected only when they are no longer used. If performance is a major concern, it can be an issue.
 
Elements in a tuple do not have names
 
We can access tuples by using the names Item1, Item2, etc. that are not meaningful at all. This representation makes it a poor choice in public APIs.
 
A maximum of eight properties can be used in a Tuple
 
If we want to return more than eight values from a method, then the last argument of the tuple must be another tuple, which makes the syntax more difficult to understand.
 
New Approach
 
In C# 7.0, tuples can be declared as “inline”, which is like an anonymous type.
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         (stringstring) tuple = GetFullName();  
  4.         Console.WriteLine($ "First Name {tuple.Item1} and Last Name {tuple.Item2}");  
  5.         Console.ReadKey();  
  6.     }  
  7.     private static(stringstring) GetFullName() {  
  8.         return ("Prasad""Raveendran");  
  9.     }  
  10. }  
This is a better approach than the old approach (improved drawback 1).
 
We can also give specific names to the tuples returning values.
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         var tuple = GetFullName();  
  4.         Console.WriteLine($ "First Name {tuple.firstName} and Last Name {tuple.lastName}");  
  5.         Console.ReadKey();  
  6.     }  
  7.     private static(string firstName, string lastName) GetFullName() {  
  8.         string firstName = "Prasad";  
  9.         string lastName = "Raveendran";  
  10.         return (firstName, lastName);  
  11.     }  
  12. }  
In the above example, we have provided names for the tuple parameters.
 
Another approach is that we can provide an explicit name while storing the result.
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         var (FirstName, LastName) = GetFullName();  
  4.         Console.WriteLine($ "First Name {FirstName} and Last Name {LastName}");  
  5.         Console.ReadKey();  
  6.     }  
  7.     private static(string firstName, string lastName) GetFullName() {  
  8.         string firstName = "Prasad";  
  9.         string lastName = "Raveendran";  
  10.         return (firstName, lastName);  
  11.     }  
  12. }  
Notes
  • Tuples are values, so they are copied by values, rather than by reference.
  • As tuples in C# 7.0 are values, so modifying a copy will not change the original copy.
  • Tuples work best with private and internal methods.
  • Stick to user-defined classes or structs for your public APIs.

Discards in Tuple

 
Starting with C# 7.0, C# supports discards, which are temporary, dummy variables that are intentionally unused in application code. Discards are equivalent to unassigned variables; they do not have a value.
 
You indicate that a variable is a discard by assigning it the underscore (_) as its name. The following method call returns a 3-tuple in which the first and second values are discards
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         var (_, _, FullName) = DiscardExample();  
  4.         Console.WriteLine($ "FullName :{FullName} ");  
  5.         Console.ReadKey();  
  6.     }  
  7.     private static(stringstringstring) DiscardExample() {  
  8.         return ("""""Prasad Raveendran");  
  9.     }  
  10. }  
In the upcoming tutorial, we will discuss the below topics,
  • Ref Locals and the Ref Return 
  • Local Functions 
  • Pattern Matching 
  • Improved Features,

    • Expression-Bodied Members, 
    • throw Expressions,
    • Numeric Literals,
    • Out Parameters -> Out Variables 
    • Out Wildcards