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
- class Program {
- static void Main(string[] args) {
- Tuple < string, string > tuple = GetFullName();
- Console.WriteLine($ "First Name {tuple.Item1} and Last Name {tuple.Item2}");
- Console.ReadKey();
- }
- private static Tuple < string, string > GetFullName() {
- //Creating an object of Tuple class by calling the static Create method
- Tuple < string, string > t = Tuple.Create("Prasad", "Raveendran");
- //Returning the tuple instance
- return t;
- }
- }
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.
- class Program {
- static void Main(string[] args) {
- (string, string) tuple = GetFullName();
- Console.WriteLine($ "First Name {tuple.Item1} and Last Name {tuple.Item2}");
- Console.ReadKey();
- }
- private static(string, string) GetFullName() {
- return ("Prasad", "Raveendran");
- }
- }

Join the conversation! Your thoughts help the community grow.