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

发布于 2024-10-30 07:45:54 字数 456 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

初雪 2024-11-06 07:45:55

不。如果元素相关,您应该返回更有用的数据类型,例如类。

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

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;
}
不气馁 2024-11-06 07:45:55

除了其他答案中指出的可能性之外,还有更多方法可以实现这一点:

  1. 创建您自己的返回类型(类似于 Tuple 方法)并返回它的实例
  2. 引用参数: http://msdn.microsoft.com/en-us/library/14akc2c7%28v=vs .80%29.aspx

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
心凉 2024-11-06 07:45:55

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

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

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

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.

昇り龍 2024-11-06 07:45:55

您可以像这样做您想做的事:

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

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);
}
错々过的事 2024-11-06 07:45:54

您可以使用 C# 4.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);
}

System.Tuple 还具有与 F# 本机元组类型互操作的好处(嗯,它是 F# 的本机元组类型,只是恰好有 F# - 用于声明元组和返回元组的函数的特定语法支持)。

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

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.

冷︶言冷语的世界 2024-11-06 07:45:54

不,只需使用 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;
}

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;
}
吻风 2024-11-06 07:45:54

根据@James Webster的建议,您可以使用元组或可以使用dynamicExpandoObject

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文