Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return a C# tuple from a method
A tuple in C# is a data structure that can hold multiple values of different types. You can return a tuple from a method, which is useful when you need to return multiple values from a single method call.
There are two main ways to return tuples from methods in C# − using the Tuple class and using the newer value tuples with more concise syntax.
Syntax
Using the Tuple class −
static Tuple<int, int, string> MethodName() {
return Tuple.Create(value1, value2, value3);
}
Using value tuples (C# 7.0 and later) −
static (int, int, string) MethodName() {
return (value1, value2, value3);
}
Using named tuple elements −
static (int age, string name, double salary) GetEmployeeInfo() {
return (25, "John", 50000.0);
}
Using Tuple Class
The traditional way to return tuples uses the Tuple class with Tuple.Create() method −
using System;
public class Demo {
public static void Main() {
var tuple = Show();
Console.WriteLine("Item1: " + tuple.Item1);
Console.WriteLine("Item2: " + tuple.Item2);
Console.WriteLine("Item3: " + tuple.Item3);
Console.WriteLine("Item4: " + tuple.Item4);
Console.WriteLine("Item5: " + tuple.Item5);
}
static Tuple<int, int, int, int, int> Show() {
return Tuple.Create(3, 5, 7, 9, 11);
}
}
The output of the above code is −
Item1: 3 Item2: 5 Item3: 7 Item4: 9 Item5: 11
Using Value Tuples
Value tuples provide a more concise syntax and better performance compared to reference tuples −
using System;
public class Demo {
public static void Main() {
var result = GetPersonInfo();
Console.WriteLine($"Name: {result.Item1}");
Console.WriteLine($"Age: {result.Item2}");
Console.WriteLine($"City: {result.Item3}");
// Deconstruction
var (name, age, city) = GetPersonInfo();
Console.WriteLine($"Deconstructed - Name: {name}, Age: {age}, City: {city}");
}
static (string, int, string) GetPersonInfo() {
return ("Alice", 28, "New York");
}
}
The output of the above code is −
Name: Alice Age: 28 City: New York Deconstructed - Name: Alice, Age: 28, City: New York
Using Named Tuple Elements
Named tuple elements make the code more readable by providing meaningful names instead of Item1, Item2, etc. −
using System;
public class Demo {
public static void Main() {
var employee = GetEmployeeDetails();
Console.WriteLine($"ID: {employee.id}");
Console.WriteLine($"Name: {employee.name}");
Console.WriteLine($"Department: {employee.department}");
Console.WriteLine($"Salary: ${employee.salary:F2}");
}
static (int id, string name, string department, double salary) GetEmployeeDetails() {
return (101, "Bob Johnson", "Engineering", 75000.50);
}
}
The output of the above code is −
ID: 101 Name: Bob Johnson Department: Engineering Salary: $75000.50
Comparison of Approaches
| Feature | Tuple Class | Value Tuples |
|---|---|---|
| Syntax | Tuple.Create() | (value1, value2) |
| Performance | Reference type (heap allocated) | Value type (stack allocated) |
| Element Access | Item1, Item2, etc. | Item1, Item2 or named elements |
| C# Version | Available since C# 4.0 | Available since C# 7.0 |
Conclusion
Returning tuples from methods in C# provides an efficient way to return multiple values. Value tuples with named elements offer the best combination of performance, readability, and modern syntax, making them the preferred choice for new C# applications.
