-3

Today I faced one question that which are the possible ways to return more than one values in function in C#

1 is returning object but what are other possible ways of doing it

Regards

Wesley Lomax
  • 2,067
  • 2
  • 20
  • 34
  • 2
    Refer this http://stackoverflow.com/questions/748062/how-can-i-return-multiple-values-from-a-function-in-c – Sankar Sep 24 '15 at 12:00
  • 1
    You are either returning a value/struct or a reference. Theoretically you cannot return more, but you can instruct the method to assign a value by reference with the `ref` or `out` keyword. – Silvermind Sep 24 '15 at 12:00

2 Answers2

2

You can use output parameters:

public void Foo(out int bar, out string goo)
{
    bar = 1;
    goo = "moo";
}

And use it like this:

int a;
string b;
Foo(out a, out b);
Kędrzu
  • 2,385
  • 13
  • 22
1

You can use a Tuple to return more than one value. Keep in mind, they can get out of hand pretty quick.

public Tuple<string, int> Foo()
{
     return Tuple.Create("Bar",1);
}

Tupples can be spun up with multiple values.

Tuple<int,string,int,int,string>  

They can get messy quick :)

Good luck

Botonomous
  • 1,746
  • 1
  • 16
  • 39