views:

9845

answers:

9

I read the C++ version of this question but didn't really understand it.

Can someone please explain clearly if it can be done and how?

Thanks,

Ash

+6  A: 

If you mean returning multiple values, you can either return a class/struct containing the values you want to return, or use the "out" keyword on your parameters, like so:

public void Foo(int input, out int output1, out string output2, out string errors) {
    // set out parameters inside function
}
Chris Doggett
Awesome thankyou!
Ash
+3  A: 

You either return a class instance our use out parameters. Here's an example of out parameters:

void mymethod(out int param1, out int param2)
{
    param1 = 10;
    param2 = 20;
}

Call it like this:

int i, j;
mymethod(out i, out j);
// i will be 20 and j will be 10
Keltex
Ohh so dont use a function?
Ash
Remember, though that just because you can, doesn't mean you should do this. This is widely accepted as a bad practice in .Net in most cases.
Michael Meadows
+9  A: 

You cannot do this in C#. What you can do is have a out parameter or return your own class (or struct if you want it to be immutable).

Using out parameter
public int GetDay(DateTime date, out string name)
{
  // ...
}
Using custom class (or struct)
public DayOfWeek GetDay(DateTime date)
{
  // ...
}

public class DayOfWeek
{
  public int Day { get; set; }
  public string Name { get; set; }
}
Samuel
An alternative in this case is to use a struct instead of a class for the return type. If the return value is stateless and transient, struct is a better choice.
Michael Meadows
+2  A: 

Classes, Structures, Collections and Arrays can contain multiple values. Output and reference parameters can also be set in a function. Return multiple values is possible in dynamic and functional languages by means of tuples, but not in C#.

Jose Basilio
+1  A: 

In C# 4, you will be able to use built-in support for tuples to handle this easily.

In the meantime, there are two options.

First, you can use ref or out parameters to assign values to your parameters, which get passed back to the calling routine.

This looks like:

void myFunction(ref int setMe, out int youMustSetMe);

Second, you can wrap up your return values into a structure or class, and pass them back as members of that structure. KeyValuePair works well for 2 - for more than 2 you would need a custom class or struct.

Reed Copsey
+3  A: 

Previous poster is right. You cannot return multiple values from a C# method. However, you do have a couple of options:

  • Return a structure that contains multiple members
  • Return an instance of a class
  • Use output parameters (using the out or ref keywords)
  • Use a dictionary or key-value pair as output

The pros and cons here are often hard to figure out. If you return a structure, make sure it's small because structs are value type and passed on the stack. If you return an instance of a class, there are some design patterns here that you might want to use to avoid causing problems - members of classes can be modified because C# passes objects by reference (you don't have ByVal like you did in VB).

Finally you can use output parameters but I would limit the use of this to scenarios when you only have a couple (like 3 or less) of parameters - otherwise things get ugly and hard to maintain. Also, the use of output parameters can be an inhibitor to agility because your method signature will have to change every time you need to add something to the return value whereas returning a struct or class instance you can add members without modifying the method signature.

From an architectural standpoint I would recommend against using key-value pairs or dictionaries. I find this style of coding requires "secret knowledge" in code that consumes the method. It must know ahead of time what the keys are going to be and what the values mean and if the developer working on the internal implementation changes the way the dictionary or KVP is created, it could easily create a failure cascade throughout the entire application.

Kevin Hoffman
+1  A: 

No, you can't return multiple values from a function in C#, at least not in the way you can do it in Python.

However, there are a couple alternatives:

You can return an array of type object with the multiple values you want in it.

private object[] DoSomething()
{
    return new [] { 'value1', 'value2', 3 };
}

You can use out parameters.

private string DoSomething(out string outparam1, out int outparam2)
{
    outparam1 = 'value2';
    outparam2 = 3;
    return 'value1';
}
dustyburwell
+3  A: 

There are several ways to do this. You can use ref parameters:

int Foo(ref Bar bar) { }

This passes a reference to the function thereby allowing the function to modify the object in the calling code's stack. While this is not technically a "returned" value it is a way to have a function do something similar. In the code above the function would return an int and (potentially) modify bar.

Another similar approach is to use an out parameter. An out parameter is identical to a ref parameter with an additional, compiler enforced rule. This rule is that if you pass an out parameter into a function, that function is required to set its value prior to returning. Besides that rule, an out parameter works just like a ref parameter.

The final approach (and the best in most cases) is to create a type that encapsulates both values and allow the function to return that:

class FooBar 
{
    public int i { get; set; }
    public Bar b { get; set; }
}

FooBar Foo(Bar bar) { }

This final approach is simpler and easier to read and understand.

Andrew Hare
+1  A: 

Mainly two methods are there. 1. Use out/ref parameters 2. Return an Array of objects

Mahatma