In this article, let us see how to use the latest feature of Tuple in C# 7.0.
Usually when we want to return multiple values from a method, then we will create a model class with the required property and will return it as a return type from the method.
Sample code without Tuple,
- //Class with property required to return from method.
- public class ReturnTypeWithoutTuple
- {
- public int _int { get; set; }
- public List<long> _longlist { get; set; }
- }
- // Class with method return the ReturnTypeWithoutTuple
- public class WithoutTupleApplication
- {
- public ReturnTypeWithoutTuple WithoutTuple()
- {
- ReturnTypeWithoutTuple returnTypeWithoutTuple = new ReturnTypeWithoutTuple();
- returnTypeWithoutTuple._int = 10;
- returnTypeWithoutTuple._longlist.Add(1);
- returnTypeWithoutTuple._longlist.Add(2);
- return returnTypeWithoutTuple;
- }
- }
- static void Main(string[] args)
- {
- WithoutTupleApplication withoutTupleApp = new WithoutTupleApplication();
- //invoking the WithoutTuple method and getting the data here.
- ReturnTypeWithoutTuple _data = withoutTupleApp.WithoutTuple();
- //perform required logic with _data
- }
But with the latest feature in C# 7, we don’t need to create separate model class anymore for every return type. Instead, let us see how we can use the tuple feature to do the same functionality of the above code.
To use the tuple in the application, first, we have to add the System.ValueTuple from NuGet package.
Sample Code with Tuple along with custom names as a return type,

Code AlonePosted Sep 19, 2018, 4:50 AM
Good Article and Good start.