C# 元组不返回值
C# Tuple not returning value
这是我第一次使用元组,所以我可能做错了什么或者只是漏掉了一些小东西。我曾经有重复的代码:
if (!B1Fturn)
{
if (B1turn)
{
FixCall(b1Status, ref b1Call, ref b1Raise, 1);
FixCall(b1Status, ref b1Call, ref b1Raise, 2);
Rules(2, 3, "Bot 1", ref b1Type, ref b1Power, B1Fturn, b1Status);
AutoCloseMsb.Show("Bot 1 Turn", "Turns", thinkTime);
AI(2, 3, ref bot1Chips, ref B1turn, ref B1Fturn, b1Status, b1Power, b1Type);
turnCount++;
B1turn = false;
B2turn = true;
}
}
if (B1Fturn && !b1Folded)
{
bools.RemoveAt(1);
bools.Insert(1, null);
maxLeft--;
b1Folded = true;
}
if (B1Fturn || !B1turn)
{
await CheckRaise(1, 1);
B2turn = true;
}
我已经复制并粘贴了 5 次,所以我决定将其全部放入一个方法中,然后只粘贴该方法 5 次。所以我开始这样做,但我意识到我需要等待一些方法,所以我选择了 async Task<some input>
但是我需要取回一些值,一些布尔值和 int 的 .2 选项 return 或使用 ref/out。 Ref/out 无论如何都不允许在异步方法中使用,因此只剩下 return。事情是我需要一些不同类型的数据来 returned 所以我需要一些东西来做到这一点(它是元组)。我使用元组创建了一种方法:
async Task<Tuple<bool, bool, bool>> Rotating(bool tempTurn, bool permaTurn, bool folded, string name, Label Status, int botCall, int botRaise, int start, int end, int current, bool next, double power, double type, int chips)
{
if (!permaTurn)
{
if (tempTurn)
{
FixCall(Status, ref botCall, ref botRaise, current);
FixCall(Status, ref botCall, ref botRaise, start);
Rules(start, end, name, ref type, ref power, permaTurn,Status);
AutoCloseMsb.Show(name + " Turn", "Turns", thinkTime);
AI(start, end, ref chips, ref tempTurn, ref permaTurn, Status, power, type);
turnCount++;
tempTurn = false;
next = true;
}
}
if (permaTurn && !folded)
{
bools.RemoveAt(current);
bools.Insert(current, null);
maxLeft--;
folded = true;
}
if (permaTurn || !tempTurn)
{
await CheckRaise(current, current);
next = true;
}
return new Tuple<bool, bool, bool>(tempTurn, permaTurn, next);
}
如您所见,上面写着 return bool,bool,bool,然后是我需要获取的值。但是,如果我在 return
行上放置断点,然后从 false 变为 true,我没有很好地解决 next 没有得到它的值更改,但是一旦它退出,它就会返回默认 false 的方法。其他 2 个布尔值也是如此。为什么 ?我做错了什么如何解决这个问题。
- P.S 我还需要 return 一些 int,但为了简单起见,我删除了它们。
在聊天对话后编辑:
您可以等待将返回一个元组的异步函数,像这样使用它:
var r = await Test (x, y); // x,y for example, Test returns Tuple
Now you can access r.Item1 and r.Item2
这是我第一次使用元组,所以我可能做错了什么或者只是漏掉了一些小东西。我曾经有重复的代码:
if (!B1Fturn)
{
if (B1turn)
{
FixCall(b1Status, ref b1Call, ref b1Raise, 1);
FixCall(b1Status, ref b1Call, ref b1Raise, 2);
Rules(2, 3, "Bot 1", ref b1Type, ref b1Power, B1Fturn, b1Status);
AutoCloseMsb.Show("Bot 1 Turn", "Turns", thinkTime);
AI(2, 3, ref bot1Chips, ref B1turn, ref B1Fturn, b1Status, b1Power, b1Type);
turnCount++;
B1turn = false;
B2turn = true;
}
}
if (B1Fturn && !b1Folded)
{
bools.RemoveAt(1);
bools.Insert(1, null);
maxLeft--;
b1Folded = true;
}
if (B1Fturn || !B1turn)
{
await CheckRaise(1, 1);
B2turn = true;
}
我已经复制并粘贴了 5 次,所以我决定将其全部放入一个方法中,然后只粘贴该方法 5 次。所以我开始这样做,但我意识到我需要等待一些方法,所以我选择了 async Task<some input>
但是我需要取回一些值,一些布尔值和 int 的 .2 选项 return 或使用 ref/out。 Ref/out 无论如何都不允许在异步方法中使用,因此只剩下 return。事情是我需要一些不同类型的数据来 returned 所以我需要一些东西来做到这一点(它是元组)。我使用元组创建了一种方法:
async Task<Tuple<bool, bool, bool>> Rotating(bool tempTurn, bool permaTurn, bool folded, string name, Label Status, int botCall, int botRaise, int start, int end, int current, bool next, double power, double type, int chips)
{
if (!permaTurn)
{
if (tempTurn)
{
FixCall(Status, ref botCall, ref botRaise, current);
FixCall(Status, ref botCall, ref botRaise, start);
Rules(start, end, name, ref type, ref power, permaTurn,Status);
AutoCloseMsb.Show(name + " Turn", "Turns", thinkTime);
AI(start, end, ref chips, ref tempTurn, ref permaTurn, Status, power, type);
turnCount++;
tempTurn = false;
next = true;
}
}
if (permaTurn && !folded)
{
bools.RemoveAt(current);
bools.Insert(current, null);
maxLeft--;
folded = true;
}
if (permaTurn || !tempTurn)
{
await CheckRaise(current, current);
next = true;
}
return new Tuple<bool, bool, bool>(tempTurn, permaTurn, next);
}
如您所见,上面写着 return bool,bool,bool,然后是我需要获取的值。但是,如果我在 return
行上放置断点,然后从 false 变为 true,我没有很好地解决 next 没有得到它的值更改,但是一旦它退出,它就会返回默认 false 的方法。其他 2 个布尔值也是如此。为什么 ?我做错了什么如何解决这个问题。
- P.S 我还需要 return 一些 int,但为了简单起见,我删除了它们。
在聊天对话后编辑: 您可以等待将返回一个元组的异步函数,像这样使用它:
var r = await Test (x, y); // x,y for example, Test returns Tuple
Now you can access r.Item1 and r.Item2