C# 具有多个返回参数的C方法

C# 具有多个返回参数的C方法,c#,.net,language-design,C#,.net,Language Design,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); } 用法: public void Main(string[] args) {

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);
}
用法:

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

    Console.WriteLine(firstName + ", " + lastName);
}
不,只需使用out参数

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;
}

您可以这样做:

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;
}
public void Main(string[] args)
{
    string firstName, lastName;
    GetFirstNameAndLastName(1, out firstName, out lastName);

    Console.WriteLine(firstName + ", " + lastName);
}
然后这样称呼它:

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;
}
public void Main(string[] args)
{
    string firstName, lastName;
    GetFirstNameAndLastName(1, out firstName, out lastName);

    Console.WriteLine(firstName + ", " + lastName);
}

您可以在C4.0中使用Tuple以轻量级的方式实现这一点

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);
}
Tuple还具有与F原生Tuple类型互操作的优点,它是F的原生Tuple类型,只是碰巧有F特定的语法支持来声明Tuple和返回它们的函数


鉴于这里存在多种方法:System.Tuple、多个out参数或返回具有FirstName和LastName属性的POCO,我怀疑Anders Hejlsberg可能不会添加对多个返回值的显式支持。

否。如果元素相关,则应返回更有用的数据类型,如类

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;
}

除了在其他答案中指出的可能性之外,还有更多的方法来证明这一点:

创建自己的返回类型,类似于Tuple方法,并返回它的一个实例 参考参数: 正如所建议的,您可以使用tuple,也可以使用dynamic和ExpandoObject


视情况而定。在你的情况下,你可以返回完整的个人记录

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

或者,如果情况需要,您可以创建自己的数据类型。

@James我相信您可以使用Tuple.Createp.FirstName,p.LastName来避免指定泛型参数。是的,刚刚更新以反映这一点-@James:元组唯一的问题是用于获取组件的.Item1和.Item2属性。使代码的可读性稍差。。。。特别是因为它们从1开始计数,而不是像数组f.e.System.Tuple那样从0开始计数。Tuple还具有与f本机元组类型互操作的优点。好吧,它是f的本机元组类型,声明元组和返回元组的函数时,恰好有特定于F的语法支持。您的函数签名看起来很可疑。我看不出其他答案的原因。这一个答案似乎很明显、简单且直截了当。@user492238:是的,复制并粘贴到那里有点让人着迷。:-为什么需要将firstName和lastName作为单独的值返回?它们不应该在Person实例中表示吗?FirstName和LastName是Person对象的属性。我喜欢这个解决方案。它简单易懂。明显的缺点是没有静态类型检查。@Jonathan Parker如果你没有什么东西可以返回,那么你就不必太担心了。谁在这方面做了a-1?我不是一个大风扇的参数,但我想听到从下来的选民。