3

Is there a need for multiple return parameters in c#/.net?

public string, string GetFirstNameAndLastName(int id)
{
    var person = from p in People
                 where p.Id = id
                 select p;
    return(p.FirstName, p.LastName);
}

Usage:

public void Main(string[] args)
{
    string firstName, lastName;
    (firstName, lastName) = GetFirstNameAndLastName(1);

    Console.WriteLine(firstName + ", " + lastName);
}
Jonathan Parker
  • 6,705
  • 3
  • 43
  • 54
  • Why do you need to return the firstName & lastName as separate values? Shouldn't they be represented in a Person instance? FirstName and LastName are properties of a Person object. – Devendra D. Chavan Apr 06 '11 at 04:44

7 Answers7

7

You can achieve this in a lightweight fashion using Tuple in C# 4.0

public Tuple<string, string> GetFirstNameAndLastName(int id)
{
   var person = from p in People
             where p.Id = id
             select p;
   return new Tuple<string, string>(p.FirstName, p.LastName);

   // OR
   // return Tuple.Create(p.FirstName, p.LastName);
}

System.Tuple also has the benefit of interoperating with F# native tuple type (well, it IS F#'s native tuple type, there just happens to be F#-specific syntax support for declaring tuples and functions that return them).

Given the existence of multiple approaches here: System.Tuple, multiple out parameters or returning a POCO with FirstName and LastName properties, I suspect that Anders Hejlsberg is probably not going to add explicit support for multiple return values.

James Webster
  • 4,046
  • 29
  • 41
  • @James I believe you can use Tuple.Create(p.FirstName, p.LastName) to avoid having to specify the generic parameters. – Matt Hamilton Apr 06 '11 at 04:35
  • Yep, just updated to reflect that ;-) – James Webster Apr 06 '11 at 04:35
  • @James: The only problem with Tuples is the nasty `.Item1` and `.Item2` properties to get the components. Makes the code slightly less readable. – Andrew Cooper Apr 06 '11 at 04:37
  • ... especially since they start counting from 1, an not from 0 like common for arrays f.e. – user492238 Apr 06 '11 at 04:40
  • `System.Tuple` also has the benefit of interoperating with F# native tuple type (well, it IS F#'s native tuple type, there just happens to be F#-specific syntax suppotr for declaring tuples and functions that return them. – James Webster Apr 06 '11 at 04:56
  • I know abut Tuple but it's so ugly to use. It gives no indication to the code using the method of what the tuple contains. – Jonathan Parker Apr 07 '11 at 00:49
5

No, just use out parameters.

public void GetFirstNameAndLastName(int id, out string first, out string last)
{
    var person = from p in People
                 where p.Id = id
                 select p;
    first = p.FirstName;
    last = p.LastName;
}
James Webster
  • 4,046
  • 29
  • 41
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
4

As suggested by @James Webster you can use tuple or you can use dynamic and ExpandoObject

class Program
{
    static void Main(string[] args)
    {
        var d = GetUserDynamic();
        Console.WriteLine("{0}.{1}", d.FirstName, d.LastName);
    }

    private static dynamic GetUserDynamic()
    {
        dynamic d = new ExpandoObject();
        d.FirstName = "amandeep";
        d.LastName = "tur";
        return d;
    }
}
Community
  • 1
  • 1
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
1

No. You should return a more useful data type like a class if the elements are related.

public MyPersonClassGetFirstNameAndLastName(int id)
{
    var person = from p in People
                 where p.Id = id
                 select p;

    MyPersonClassreturnValue = new MyPersonClass;
    returnValue.FirstName = p.FirstName; 
    returnValue.LastName= p.LastName;
    return returnValue;
}
Fellmeister
  • 591
  • 3
  • 24
0

Next to the possibilities pointed out in other answers, there is are more ways to archieve this:

  1. create your own return type (similar to the Tuple method) and return an instance of it
  2. ref parameters: http://msdn.microsoft.com/en-us/library/14akc2c7%28v=vs.80%29.aspx
user492238
  • 4,094
  • 1
  • 20
  • 26
0

Depends on the situation. In your case you could return the whole person record back,

public string, string GetFirstNameAndLastName(int id)
{
    var person = from p in People
             where p.Id = id
             select p;
    return person;
}

or if the situation required it you could create your own data type.

peter
  • 13,009
  • 22
  • 82
  • 142
-1

You can do what you want like this:

public void GetFirstNameAndLastName(int id, out string firstName, out string lastName)
{
    var person = from p in People
                 where p.Id = id
                 select p;
    firstName = p.FirstName;
    lastName =  p.LastName;
}

and then call it like this:

public void Main(string[] args)
{
    string firstName, lastName;
    GetFirstNameAndLastName(1, out firstName, out lastName);

    Console.WriteLine(firstName + ", " + lastName);
}
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Who did a -1 on this? I am not a big fan of out parameters, but I would like to hear from the down voter. – peter Apr 07 '11 at 23:21