C# methods with multiple return parameters [duplicate]
Is there a need for multiple return parameters in c#/.net?
public string, string GetFirstNameAndLastName(int id)
{
var person = from p in Peop开发者_JS百科le
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);
}
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.
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;
}
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;
}
}
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;
}
Next to the possibilities pointed out in other answers, there is are more ways to archieve this:
- create your own return type (similar to the Tuple method) and return an instance of it
- ref parameters: http://msdn.microsoft.com/en-us/library/14akc2c7%28v=vs.80%29.aspx
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.
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);
}
精彩评论