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
How to return multiple values to caller method in c#?
Returning multiple values from a method in C# can be achieved through several approaches. The most modern and efficient approach is using ValueTuple, introduced in C# 7.0, which provides a lightweight mechanism for returning multiple values with optional named elements.
ValueTuples are both performant and allow referencing by names the programmer chooses. They are available under the System.ValueTuple NuGet package for older framework versions.
Syntax
Following is the syntax for declaring a method that returns multiple values using ValueTuple −
public (int, string, string) MethodName() {
return (value1, value2, value3);
}
Following is the syntax for named tuple elements −
public (int Id, string Name, string Size) MethodName() {
return (Id: 1, Name: "Apple", Size: "Big");
}
Using ValueTuple with Item Properties
The simplest way to return multiple values is using ValueTuple and accessing values through Item1, Item2, etc. −
using System;
namespace DemoApplication {
class Program {
public static void Main() {
var fruits = GetFruits();
Console.WriteLine($"Fruit Id: {fruits.Item1}, Name: {fruits.Item2}, Size: {fruits.Item3}");
}
static (int, string, string) GetFruits() {
return (1, "Apple", "Big");
}
}
}
The output of the above code is −
Fruit Id: 1, Name: Apple, Size: Big
Using Named Tuple Elements
You can provide meaningful names to tuple elements, making the code more readable −
using System;
namespace DemoApplication {
class Program {
public static void Main() {
var fruits = GetFruits();
Console.WriteLine($"Fruit Id: {fruits.Id}, Name: {fruits.Name}, Size: {fruits.Size}");
}
static (int Id, string Name, string Size) GetFruits() {
return (Id: 1, Name: "Apple", Size: "Big");
}
}
}
The output of the above code is −
Fruit Id: 1, Name: Apple, Size: Big
Using Tuple Deconstruction
You can deconstruct the returned tuple directly into individual variables −
using System;
namespace DemoApplication {
class Program {
public static void Main() {
(int fruitId, string fruitName, string fruitSize) = GetFruits();
Console.WriteLine($"Fruit Id: {fruitId}, Name: {fruitName}, Size: {fruitSize}");
}
static (int Id, string Name, string Size) GetFruits() {
return (Id: 1, Name: "Apple", Size: "Big");
}
}
}
The output of the above code is −
Fruit Id: 1, Name: Apple, Size: Big
Alternative Approaches
| Approach | Description | Best Used When |
|---|---|---|
| ValueTuple | Lightweight, modern approach with named elements | Returning 2-7 related values |
| out parameters | Pass parameters by reference to return additional values | One primary return value plus extras |
| Custom class/struct | Create a dedicated type to hold multiple values | Complex data with behavior or many fields |
Using out Parameters Example
using System;
namespace DemoApplication {
class Program {
public static void Main() {
string name, size;
int id = GetFruitInfo(out name, out size);
Console.WriteLine($"Fruit Id: {id}, Name: {name}, Size: {size}");
}
static int GetFruitInfo(out string name, out string size) {
name = "Apple";
size = "Big";
return 1;
}
}
}
The output of the above code is −
Fruit Id: 1, Name: Apple, Size: Big
Conclusion
ValueTuple is the recommended approach for returning multiple values in modern C#, offering better performance and readability than traditional methods. Use named tuple elements and deconstruction for cleaner, more maintainable code when working with multiple return values.
