20

我有一个计算两个位置的函数,我想同时获得它们,有没有办法从同一个函数返回两个值,而不会将它们变成一个数组。我认为一些有争议的东西或类似的东西...... 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()
{
}
4

10 回答 10

33

有多种方法:

1)用途:

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

或者

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

2)使用自定义类,如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;
     }      
 }

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

以下是这些方法的比较:multiple-return-values

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

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);
}
于 2013-01-24T08:03:31.793 回答
13

使用结构或类:

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关键字更漂亮。

于 2013-01-24T07:54:12.507 回答
6

您不能以这种方式返回 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);

它将用计算值填充它们。

于 2013-01-24T07:52:40.160 回答
6

从 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}.");

甚至有名字:

(string first, string middle, string last) LookupName(long id)

var names = LookupName(id);
WriteLine($"found {names.first} {names.last}.");

更多信息可以在这里找到。

于 2016-08-27T16:27:30.167 回答
3

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

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;
}
于 2013-01-24T07:59:19.737 回答
2

您可以使用输出、坐标结构或元组。

使用元组:

public Tuple<int, int> GetLocation()
{
     return new Tuple<int,int>(1,2);
}
于 2013-01-24T08:00:17.900 回答
2

如果您使用的是 windows 窗体,则可以使用Pointstruct;

public static Point Location(Point p1, Point p2)
{
    return new Point(p2.X - p1.X, p2.Y - p1.Y);
}   
于 2013-01-24T08:01:25.647 回答
1

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

private Tuple<int, string> Foo()
{
  // ...
}
于 2013-01-24T07:59:36.050 回答
1

您可以使用 aTuple<T1, T2>或使用 out 参数。

于 2013-01-24T17:53:25.470 回答
0

使用数组列表

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;
 }
于 2013-01-24T08:37:08.603 回答