6

Hii

I have method in C#, I have to return multiple values from that method with out using collections like arrays. Is there any reliable way ?

Jibu P C_Adoor
  • 3,304
  • 9
  • 30
  • 36
  • Can you please expand on "multiple values"? Is it a constant number of your return values? Are they linked logically? What to they represent? Are they in the same type? – Kobi May 27 '10 at 06:30
  • Possible duplicate of [How can I return multiple values from a function in C#?](http://stackoverflow.com/questions/748062/how-can-i-return-multiple-values-from-a-function-in-c) – Michael Freidgeim Jun 03 '16 at 23:18

7 Answers7

15

Yes, the out keyword:

public void ReturnManyInts(out int int1, out int int2, out int int3)
{
    int1 = 10;
    int2 = 20;
    int3 = 30;
}

then call it like this:

int i1, i2, i3;
ReturnManyInts(out i1, out i2, out i3);

Console.WriteLine(i1);
Console.WriteLine(i2);
Console.WriteLine(i3);

which outputs:

10
20
30

EDIT:

I'm seeing that a lot of posts are suggesting to create your own class for this. This is not necessary as .net provides you with a class to do what they are saying already. The Tuple class.

public Tuple<int, string, char> ReturnMany()
{
    return new Tuple<int, string, char>(1, "some string", 'B');
}

then you can retrieve it like so:

var myTuple = ReturnMany();
myTuple.Item1 ...
myTuple.Item2 ...

there are generic overloads so you can have up to 8 unique types in your tuple.

Joel
  • 16,474
  • 17
  • 72
  • 93
10

Well, you could use:

  • a custom class/struct/type, containing all your values
  • out parameters

I.e.:

class MyValues
{
    public string Val1 { get; set; }
    public int Val2 {get; set; }
}

public MyValues ReturnMyValues();

or

public void ReturnMyValues(out string Val1, out int Val2);
Kyle Rosendo
  • 25,001
  • 7
  • 80
  • 118
5

If you are using .NET 4.0 you can use one of the generic Tuple classes to return multiple values from a method call. The static Tuple class provides methods to create Tuple objects. So you do not have to define your own return type for the method.

public Tuple<string,int> Execute()
{
  return new Tuple<string,int>("Hello World", 2);
}
Jehof
  • 34,674
  • 10
  • 123
  • 155
2

Yes could create a new type that will contain multiple properties and then return this type:

public MyType MyMethod() 
{
    return new MyType
    {
        Prop1 = "foo",
        Prop2 = "bar"
    };
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

Why should using 'out' being an unreliable way? (Or did you make a typo and meant without?)

There are several methods:

  • Return a object which holds multiple values (struct/class etc)
  • out
  • ref
RvdK
  • 19,580
  • 4
  • 64
  • 107
0

A Descriptor class, or structure. You must put it somewhere as it's logical that a method must return only one value. Alternatively you can use out or ref, but i would go returning a class.

Btw what's holding you not to use collections?.

0
public int Method Name(out string stringValue, out int intValue)
{
    ///
          Method goes here
    ///

    return intVaraible
}

here you will get 3 return Values 1. stringValue 2. intValue 3. intVariable

Johnny
  • 1,555
  • 3
  • 14
  • 23