C# 如何从一个函数返回2个值

C# 如何从一个函数返回2个值,c#,C#,我有一个计算两个位置的函数,我想得到两个位置,有没有一种方法可以得到从同一个函数返回的两个值,而不用把它们转换成数组。我觉得有点不对劲之类的。。。tnx。这是我的代码: public static int Location(int p_1, int p_2, int p_3, int p_4) { int XLocation = p_2 - p_1; int YLocation = p_4-p_3; return XLocation,YLocation; } public voi

我有一个计算两个位置的函数,我想得到两个位置,有没有一种方法可以得到从同一个函数返回的两个值,而不用把它们转换成数组。我觉得有点不对劲之类的。。。tnx。这是我的代码:

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

public void Print()
{
}

不能以这种方式返回2个值。但您可以将变量作为输出变量传递,如下所示:

  public static void Location(int p_1, int p_2, int p_3, int p_4, out int XLocation, out int YLocation)
{
    XLocation = p_2 - p_1;

    YLocation = p_4-p_3;
}
然后只需将目标变量传递给方法:

int Xlocation, YLocation;
Location(int p_1, int p_2, int p_3, int p_4, out int XLocation, out int YLocation);

它将用计算值填充它们。

使用结构或类:

public struct Coordinates
{
    public readonly int x;
    public readonly int y;

    public Coordinates (int _x, int _y) 
    {
        x = _x;
        y = _y;
    }
}

public static Coordinates Location(int p_1, int p_2, int p_3, int p_4) 
{
    return new Coordinates(p_2 - p_1, p_4 - p_3);
}

我发现它比使用
out
关键字更漂亮。

运算符“return”不可能返回两个值。 您可以将以下代码与“struct”一起使用:

public static Position Location(int p_1, int p_2, int p_3, int p_4)
{

Position location;
location.xLocation = p_2 - p_1;
location.yLocation =p_4-p_3;;

return location;
}

public struct Position
{
public int xLocation;
public int yLocation;
}

如果方法不是公共合同的一部分,我更喜欢使用:

private Tuple Foo()
{
// ...
}

您可以使用out、坐标结构或元组

使用元组:

public Tuple<int, int> GetLocation()
{
     return new Tuple<int,int>(1,2);
}
公共元组GetLocation()
{
返回新的元组(1,2);
}

如果您使用的是windows窗体,则可以使用
Point
struct

public static Point Location(Point p1, Point p2)
{
    return new Point(p2.X - p1.X, p2.Y - p1.Y);
}   

实现这一点有多种方法:

1) 使用:

3) 使用
out
关键字:

   public static int Location(int p_1, int p_2, int p_3, int p_4, out int XLocation, out int YLocation)
   {
        XLocation = p_2 - p_1;    
        YLocation = p_4 - p_3;
   }
以下是这些方法的比较:

最快的方法(最佳性能)是:

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);
}
公钥值对位置(int p_1、int p_2、int p_3、int p_4)
{                 
返回新的KeyValuePair(p_2-p_1,p_4-p_3);
}

使用数组列表

public ArrayList Location(int p_1, int p_2, int p_3, int p_4)
{
 ArrayList ar = new ArrayList();
 ar.AddRange(new string[] { "", "" });  
 int  XLocation = p_2 - p_1;
 int YLocation = p_4-p_3;
 ar[0] = XLocation.ToString(); 
 ar[1] = YLocation.ToString(); 
 return ar;
 }

您可以使用
元组
或使用out参数。

从C#7.0开始,您可以使用如下元组:

(string, string) LookupName(long id) // tuple return type
{
    ... // retrieve first, middle and last from data storage
    return (first, last); // tuple literal
}

var names = LookupName(id);
WriteLine($"found {names.Item1} {names.Item2}.");
可以找到更多信息


它一直在问我返回值。。。返回什么?可变值类型是邪恶的。对于这个具体的返回值,我会接受。我也会。遗憾的是,微软提出的观点是可变的使结构不可变-如果有任何错误,请告诉我。:)应该是:返回新点(){XLocation=p_2-p_1,YLocation=p_4-p_3}@很好,你真的很好,这是迄今为止最低效的答案。
   public static int Location(int p_1, int p_2, int p_3, int p_4, out int XLocation, out int YLocation)
   {
        XLocation = p_2 - p_1;    
        YLocation = p_4 - p_3;
   }
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);
}
public ArrayList Location(int p_1, int p_2, int p_3, int p_4)
{
 ArrayList ar = new ArrayList();
 ar.AddRange(new string[] { "", "" });  
 int  XLocation = p_2 - p_1;
 int YLocation = p_4-p_3;
 ar[0] = XLocation.ToString(); 
 ar[1] = YLocation.ToString(); 
 return ar;
 }
(string, string) LookupName(long id) // tuple return type
{
    ... // retrieve first, middle and last from data storage
    return (first, last); // tuple literal
}

var names = LookupName(id);
WriteLine($"found {names.Item1} {names.Item2}.");