【问题标题】:How to return 2 or 3 values with Tuple?如何使用 Tuple 返回 2 或 3 个值?
【发布时间】:2016-12-26 14:52:41
【问题描述】:

所以我正在用 c# 制作一个愚蠢的游戏,但我有点卡在这里。

我尝试返回 2 个值或 3 个值,这取决于游戏规则。

我需要知道如何仅在一个函数中返回不同数量的值。就像有时我需要在一个函数中返回 3 个值,有时是 7 个值。

错误在于变量tuple 当然,因为我不能返回少于 3 个值..

public Tuple<int, int, int> movepion(schaken.pion pion, int row, int column)
{
    int row2 = row + 1;

    //posities berekenen
    if (pion.kleur == Color.Blue)
    {
        if (pion.volgnr == 0) { row++; row2++; }
        else { row++; }
    }
    else
    {
    }

    // declaration
    var tuple = new Tuple<int, int>(row, column);
    var tuple2 = new Tuple<int, int, int>(row, row2, column);

    // what position?
    if (pion.volgnr == 0)
    {
        pion.volgnr = pion.volgnr + 1;
        return tuple2;
    }
    else
    {
        return tuple;
    }
}

【问题讨论】:

  • 你能总结一下它的用途吗? tuple 和 tuple2 的用途是什么?
  • @Thibaut 如果所有值都是 int,您是否考虑过返回数组、IEnumerable 或列表?
  • @sr28 我想做一个国际象棋游戏,所以如果我点击一个 pion,它将显示 pion 可以放置的所有不同位置。当可以放置更多位置时,我需要更多行或列。
  • @Thibaut - 这个元组是否应该代表移动选项的多样性?
  • 您应该考虑使用单独的数据类型来表示棋盘上的位置,并在调用允许的移动时返回 List

标签: c# return tuples


【解决方案1】:

返回集合而不是 Tuple&lt;&gt;

public IEnumerable<int> movepion(schaken.pion pion, int row, int column)
{
    IEnumerable<int> result;
    bool return2Items = false; //Some logic part to decide how many items to return
    if(return2Items)
        result = new List<int> { 1, 2 };
    else
        result = new List<int> { 1, 2, 3 };

   return result;    
}

在更好地理解 cmets 之后,我建议创建一个对象 MoveOption,并且该函数将返回 IEnumerable&lt;MoveOption&gt;

public class MoveOption
{
    public int X { get; set; }
    public int Y { get; set; }
}

然后在你的函数中:

public IEnumerable<MoveOption> movepion(schaken.pion pion, int row, int column)
{
    List<MoveOption> options = new List<MoveOption>();

    if(/*some condition*/)
    {
        options.Add(new MoveOption{ X = 1, Y = 2 });
    }

    if(/*some condition*/)
    {
        options.Add(new MoveOption{ X = 5, Y = 7 });
    }

    //Rest of options

    return options;
}

如果你想更进一步,你可以使用继承,并且:

public interface IMoveOptionCalcumator
{
    public MoveOption Calculate(/*input*/);
}

public class MoveOptionX : IMoveOptionCalcumator
{
    public MoveOption Calculate(/*input*/) { /*some implementation*/ }
}

在你的函数中:

public class YourClass
{
    public IEnumerable<IMoveOptionCalcumator> MoveOptionCalculators { get; set; }

    public YourClass()
    {
        // Initialize the collection of calculators 
        // You can look into Dependency Injection if you want even another step forward :)
    }


    public IEnumerable<int> movepion(schaken.pion pion, int row, int column)
    {
        List<MoveOption> options = new List<MoveOption>();
        foreach (var item in MoveOptionsCalculators)
        {
            var result = item.Calculate(/*input*/);
            if(result != null)
            {
                options.Add(result);
            }
        }
        return options;
    }
}

【讨论】:

  • 这看起来很不错,我会试试这个,如果它有效,请告诉你!
  • @Thibaut - 检查我在界面中添加的内容 - 可能是您正在寻找的更多内容
  • 我有一个奇怪的问题 new List 中的 List 给了我一个错误,我不知道为什么......我会先尝试第一种可能性,我会尝试让它进入你在这里给我的下一步。你真的在帮我!
  • @Thibaut - Ya :) 一步一步来 :) 这就是学习的方式
  • @Thibaut - 感谢您的编辑 :) 在尝试尽快输入时忘记输入它哈哈哈 :) 甚至更好地自己解决它 - 好一个
【解决方案2】:

尝试将返回类型更改为

Tuple<int, int, int?>

你会这样

var tuple = new Tuple<int, int, int?>(row, column, null);

无论如何,我认为最好清除方法的语义,并可能让它返回一个有意义的自定义数据类型。

【讨论】:

  • 这仍然返回 3 个值 - 只是其中 1 个是 null
  • 这不起作用,我无法将类型 int? 转换为 int 它说
  • 使用 .Value 从你的 Nullable 中获取 int
  • 你必须将方法的返回类型和tuple2的类型更改为与tuple相同。无论如何,最好听从 Geoffrey 的建议,返回 IEnumerable 或 IEnumerable>。
  • 这也不支持问题所说的她可能要返回 7 个值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
  • 2011-03-01
相关资源
最近更新 更多