80

Is it is possible to return multiple values from a method natively?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Coder Newbie
  • 865
  • 1
  • 6
  • 5
  • it might be helpful https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx – Darshan Faldu Mar 21 '17 at 11:51
  • 3
    The nice way is to define a `class` or a `struct` holding the "multiple values" and return an instance of one of those. You'll find that approach scales best. Tuples are a bad idea since they are too "freeform" if you get my meaning. – Bathsheba Mar 21 '17 at 11:52
  • Any guide to "new features in C# 7" would show you tuples... – Jon Skeet Mar 21 '17 at 11:53
  • 5
    @Bathsheba: In a public API, maybe (not enough time to judge yet) - but within a class or even an assembly, I think tuples are going to be *really* handy. Why bother creating your own whole type to maintain if you *just* want a collection of variables in one or two places? – Jon Skeet Mar 21 '17 at 11:54
  • 1
    Because that "just" tends to evolve to having to meet other requirements. To me tuples are going to be thoroughly misused by folk whom are precisely too indolent to create their own type. – Bathsheba Mar 21 '17 at 11:55
  • And when @JonSkeet kicks in on a .NET question, that's gg hahah – Sid Mar 21 '17 at 11:55
  • @DarshanPatel I think `tuples` fits better here – Sid Mar 21 '17 at 11:58
  • 3
    @Bathsheba: So don't misuse them, and advice against their misuse. Use them in the right place - but don't *completely reject them* just because they can be misused. Any feature can be misused, and many are. – Jon Skeet Mar 21 '17 at 12:17
  • -find a new feature -create seo friendly title -create a question - and answer it in one minute – levent Mar 21 '17 at 12:44
  • @levent know what you are answering, know where you can find docs. Make example using docs. – Sid Mar 22 '17 at 11:54
  • It should be closed as duplicate of [Return multiple values to a method caller](//stackoverflow.com/a/36436255) – Michael Freidgeim Mar 02 '21 at 07:39

3 Answers3

191

What do you mean by natively?

C# 7 has a new feature that lets you return more than one value from a method thanks to tuple types and tuple literals.

Take the following function for instance:

(string, string, string) MyCoolFunction() // tuple return type
{   
    //...        
    return (firstValue, secondValue, thirdValue);
}

Which can be used like this:

var values = MyCoolFunction();
var firstValue = values.Item1;
var secondValue = values.Item2;
var thirdValue = values.Item3;

Or by using deconstruction syntax

(string first, string second, string third) = MyCoolFunction();

//...

var (first, second, third) = MyCoolFunction(); //Implicitly Typed Variables

Take some time to check out the Documentation, they have some very good examples (this answer's one are based on them!).

Sid
  • 14,176
  • 7
  • 40
  • 48
  • 7
    Also worth to point out that you can name the return values too, which let's you deconstruct them in any order. – Jim Aho Aug 28 '17 at 10:46
  • 11
    If you have the error `Predefined type 'System.ValueTuple´2´ is not defined or imported`, look here: [stackoverflow.com/a/38383064/789423](https://stackoverflow.com/a/38383064/789423) – Beauty Oct 06 '17 at 13:14
  • 1
    One thing not mentioned above, and which had me very confused for a while, is that the deconstruction syntax can be used such that the tuple items returned by a method can be assigned to fields defined earlier in the program. And if the method is being called within an anonymous method definition then some tuple items may get assigned to fields defined outside the anonymous method and some to fields defined within the anonymous method. – RenniePet Mar 24 '18 at 12:28
  • I believe by "natively" he meant it works out of the box—included in the standard framework. Which it does, _sometimes_. Refer to this post: https://stackoverflow.com/questions/38382971/predefined-type-system-valuetuple%C2%B42%C2%B4-is-not-defined-or-imported – nardnob Jun 14 '18 at 17:20
  • And deconstruction can be shortened even more: `var (first, second, third) = MyCoolFunction();` – zmechanic Mar 21 '19 at 11:10
26

You are looking for Tuples. This is an example:

static (int count, double sum) Tally(IEnumerable<double> values)
{
    int count = 0;
    double sum = 0.0;
    foreach (var value in values)
    {
        count++;
        sum += value;
    }
    return (count, sum);
}

...

var values = ...
var t = Tally(values);
Console.WriteLine($"There are {t.count} values and their sum is {t.sum}");

Example stolen from http://www.thomaslevesque.com/2016/07/25/tuples-in-c-7/

user2657943
  • 2,598
  • 5
  • 27
  • 49
-4

You can also implement like this:

public class Program
{
    public static void Main(string[] args)
    {

        var values=GetNumbers(6,2);
        Console.Write(values);


    }

    static KeyValuePair<int,int> GetNumbers(int x,int y)
    {
        return new KeyValuePair<int,int>(x,y);
    }
}
Maganjo
  • 59
  • 2