954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to return more than one value from a method?

Hi All,
I have specific requirement in C#.
I have to return two values from a single variable/method.
How to do it?

If you can help me, you are most welcome.

Thanks and with regards,

Madhusudhan.H.K.

cumadhu
Newbie Poster
19 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

Return an array, containing the values.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

you could also use the "ref" parameter
that would change the values of the passed parameter.

plazmo
Posting Whiz in Training
207 posts since Aug 2005
Reputation Points: 23
Solved Threads: 16
 

Can i Use 'out' keyword with respect to string(as return values) insted of 'ref'.
I dont know how to work with 'out' attribute.
Can you send code sample for the same.

Thanks and with regards,
Madhusudhan

Madhusudhan
Newbie Poster
3 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

hi
you can return an address of each array avec C#,when you cashe over one value in this array , you can return over one value

halil
Newbie Poster
1 post since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

Is C# not an OO language, can you not just return an Object cantaining the two attributes. By returning a Class (Object) you have much more freedom.

Kate Albany
Junior Poster in Training
71 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

Late, I know, but -- Kate is correct. In the case that you want to return two variables of different types, you can simply return an array of object, or a Class created to contain those different types. Lets look at a couple of different ways to accomplish this:

// Old school not very OOP way to accomplish your task
public object[] MultiReturnMethodOne(string Param1, int Param2)
{
     // DO WORK
     return new object[]{result1,result2,...};
}

public YourClassName MultiReturnMethodTwo(string Param1, int Param2)
{
     // DO WORK
     YourClassName result = new YourClassName(Result1, Result2);
     return result;
}

// OOP Class used in Method 2
public class YourClassName
{
     // declare our private fields
     private string _result1=string.Empty;
     private int _result2=0;

     // constructor
     public YourClassName(string Result1, int Result2)
     {
          this._result1 = Result1;
          this._result2 = Result2;
     }

     // Gets our first Result (string)
     public string Result1
     {
          get
          {
               return _result1; 
          }
     }
     // Gets our Second Result (string)
     public int Result2
     {
          return _result2;
     }     

}

public void testMultiReturn()
{
     // TEST METHOD ONE
     object[] result = MultiReturnMethodOne("test1",10);
     // Now we have our results, but here we dont know exactly
     // what they are so we have to cast them to 
     // their respective types
     // There are more than two ways to do this, but here are the
     // most common
     // <strong>Cast One</strong> - Common but may fail
     // this method will fail if the result[0] is not a string
     // or if result[1] is not an int
     // so use this in the case that you don't have any question
     // as to the results
     string result1 = (string)result[0]; 
     int result2 = (int)result[1]; 
     // <strong>Cast Two</strong> - Safer but less informative as to why it 
     // didn't work if it fails
     // safer -- this second method will not fail, but result1 will be null
     // if the result[0] is not a string 
     // or result2 will equal its default value (0) if result[1] is not an
     // integer
   
     result1 = result[0] as string;  
     result2 = result[1] as string;
     // END TEST METHOD ONE

     // TEST METHOD TWO - <strong>Preferred Where possible</strong> 
     // As Kate said, why not use a class or a struct?
     // See above for definitions
     YourClassName ycn = MultiReturnMethodTwo("test1",10);
     // Now we have a nicer result
     Console.WriteLine("String Result {0}, Int Result {1}",
          ycn.Result1, 
          ycn.Result2);
     // Much more concise, and OOP.
}


Hope that this helps.

SethWebster
Newbie Poster
7 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

As to not leave people thinking that there is only one way. You can use the + to return more then one value.

return item1 + item2;


That will give you two values out without having to use object classes etc....

rollo1002
Newbie Poster
2 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

As to not leave people thinking that there is only one way. You can use the + to return more then one value.

return item1 + item2;
That will give you two values out without having to use object classes etc....

Ok brain fart... I was wrong... disregard this. I was trying to delete it but can't....

rollo1002
Newbie Poster
2 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

As SethWebster explained, you return an array.
If there are more value with different type (int, decimal, string, ...) you return an object array.
If there are all the values of the same type, you can return the same type:

public int[] Returnmethod()
{
    int a = 1;
    int b = 2;
    return new int[] {a , b };
}

Mitja

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

To me an array can be considered as ONE value.
A method that returns two values could be:

static int toreturn(int inint, out bool bbool)
        {
            if (inint == 42) bbool = true;
            else bbool = false;
            return inint*2;
        }

This would return the double value ofinint and [the return of B]bbool [/B]via out, would only be true if the initial value of inint is equal to 42.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
Ok brain fart... I was wrong... disregard this. I was trying to delete it but can't....

Yeah, I was thinking. I was like... Um, I'm a newbie, but even I know that that's either concatenating or adding them :D

Zephyr-
Newbie Poster
20 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Nice way to resurrect a 6 year old thread :)

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 
Nice way to resurrect a 6 year old thread :)

Lol I just realized. Plus, that wasn't my fault, rollo did it :D

Zephyr-
Newbie Poster
20 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

you can also try this:

If you have a function that sets 2 Global variables(or arrays if nesecarry) then the function doesn't need to return anything. when you need the values again you simply call them from the Globals class.

Here is an example:

public static class Globals
{
    public static string name = "";
    public static int age = 0;
}

public void setValues(string value1, int value2)
{
    Globals.name = value1;
    Globals.age = value2;
}

class program
{
    Console.WriteLine("Name: " + Globals.name);
    Console.WriteLine("Age: " + Globals.age);
}
davieJohnson
Newbie Poster
16 posts since Dec 2010
Reputation Points: 16
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You