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
Set tuple as a method parameter in C#
In C#, you can pass tuples as method parameters to group related values together. This approach is useful when you need to pass multiple values to a method without creating a separate class or using multiple parameters.
Syntax
Following is the syntax for creating a tuple and passing it as a method parameter −
// Creating a tuple
var tuple = Tuple.Create(value1, value2, value3);
// Method with tuple parameter
static void MethodName(Tuple<Type1, Type2, Type3> tuple) {
// Access tuple items using Item1, Item2, Item3
}
Using Classic Tuple as Method Parameter
The traditional Tuple.Create() method creates a tuple that can be passed to methods. Individual values are accessed using Item1, Item2, etc. −
Example
using System;
public class Program {
public static void Main() {
var tuple = Tuple.Create(100, 200, 300);
Show(tuple);
}
static void Show(Tuple<int,int,int> tuple) {
Console.WriteLine("First value: " + tuple.Item1);
Console.WriteLine("Second value: " + tuple.Item2);
Console.WriteLine("Third value: " + tuple.Item3);
}
}
The output of the above code is −
First value: 100 Second value: 200 Third value: 300
Using Value Tuples as Method Parameter
C# 7.0 introduced value tuples with named elements, which provide better performance and readability −
Example
using System;
public class Program {
public static void Main() {
var employee = (Name: "John", Age: 30, Salary: 50000);
DisplayEmployee(employee);
// You can also pass tuple directly
DisplayEmployee(("Alice", 25, 45000));
}
static void DisplayEmployee((string Name, int Age, int Salary) employee) {
Console.WriteLine($"Employee: {employee.Name}");
Console.WriteLine($"Age: {employee.Age}");
Console.WriteLine($"Salary: ${employee.Salary}");
Console.WriteLine();
}
}
The output of the above code is −
Employee: John Age: 30 Salary: $50000 Employee: Alice Age: 25 Salary: $45000
Returning Tuples from Methods
Methods can also return tuples, making it easy to return multiple values −
Example
using System;
public class Program {
public static void Main() {
var result = Calculate(10, 5);
DisplayResults(result);
}
static (int Sum, int Product, double Average) Calculate(int a, int b) {
int sum = a + b;
int product = a * b;
double average = (a + b) / 2.0;
return (sum, product, average);
}
static void DisplayResults((int Sum, int Product, double Average) result) {
Console.WriteLine($"Sum: {result.Sum}");
Console.WriteLine($"Product: {result.Product}");
Console.WriteLine($"Average: {result.Average}");
}
}
The output of the above code is −
Sum: 15 Product: 50 Average: 7.5
Comparison
| Feature | Classic Tuple | Value Tuple |
|---|---|---|
| Syntax | Tuple.Create(1, 2) | (1, 2) or (a: 1, b: 2) |
| Access | tuple.Item1, tuple.Item2 | tuple.Item1 or tuple.a |
| Performance | Reference type (slower) | Value type (faster) |
| Named elements | No | Yes |
Conclusion
Passing tuples as method parameters in C# allows you to group related values together efficiently. Value tuples (introduced in C# 7.0) provide better performance and readability compared to classic tuples, especially when using named elements for clearer code.
