C#函数返回多值

Here are basic Two methods:

1) Use of 'out' as parameter You can use 'out' for both 4.0 and minor versions too.

Example of 'out':

using System;

namespace out_parameter
{
  class Program
   {
     //Accept two input parameter and returns two out value
     public static void rect(int len, int width, out int area, out int perimeter)
      {
        area = len * width;
        perimeter = 2 * (len + width);
      }
     static void Main(string[] args)
      {
        int area, perimeter;
        // passing two parameter and getting two returning value
        Program.rect(5, 4, out area, out perimeter);
        Console.WriteLine("Area of Rectangle is {0}\t",area);
        Console.WriteLine("Perimeter of Rectangle is {0}\t", perimeter);
        Console.ReadLine();
      }
   }
}

Output:

Area of Rectangle is 20

Perimeter of Rectangle is 18

*Note:*The out-keyword describes parameters whose actual variable locations are copied onto the stack of the called method, where those same locations can be rewritten. This means that the calling method will access the changed parameter.

2) Tuple<T>

Example of Tuple:

Returning Multiple DataType values using Tuple<T>

using System;

class Program
{
    static void Main()
    {
    // Create four-item tuple; use var implicit type.
    var tuple = new Tuple<string, string[], int, int[]>("perl",
        new string[] { "java", "c#" },
        1,
        new int[] { 2, 3 });
    // Pass tuple as argument.
    M(tuple);
    }

    static void M(Tuple<string, string[], int, int[]> tuple)
    {
    // Evaluate the tuple's items.
    Console.WriteLine(tuple.Item1);
    foreach (string value in tuple.Item2)
    {
        Console.WriteLine(value);
    }
    Console.WriteLine(tuple.Item3);
    foreach (int value in tuple.Item4)
    {
        Console.WriteLine(value);
    }
    }
}

Output

perl
java
c#
1
2
3

NOTE: Use of Tuple is valid from Framework 4.0 and above.Tuple type is a class. It will be allocated in a separate location on the managed heap in memory. Once you create the Tuple, you cannot change the values of its fields. This makes the Tuple more like a struct.

3) Use:

public KeyValuePair<int, int> Location(int p_1, int p_2, int p_3, int p_4)
{                 
    return new KeyValuePair<int,int>(p_2 - p_1, p_4-p_3);
}

or

static Tuple<int, int> Location(int p_1, int p_2, int p_3, int p_4)
{
    return new Tuple<int, int>(p_2 - p_1, p_4-p_3);
}

4) Use custom class like Point

public class Point
{
    public int XLocation { get; set; }
    public int YLocation { get; set; }
}

public static Point Location(int p_1, int p_2, int p_3, int p_4) 
{    
     return new Point 
     {
        XLocation  = p_2 - p_1;
        YLocation = p_4 - p_3;
     }      
 }

The fastest way (best performance) is:

public KeyValuePair<int, int> Location(int p_1, int p_2, int p_3, int p_4)
{                 
    return new KeyValuePair<int,int>(p_2 - p_1, p_4-p_3);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值