为什么我会收到 InvalidOperationException?

发布于 2024-11-01 17:15:45 字数 513 浏览 1 评论 0原文

所以我有一个构造函数和一个返回卡片的方法。由于某种原因,我收到 InvalidOperationException。任何人都可以想到任何事情吗?

卡片列表不为空,我只是从这里的构造函数中删除了卡片生成算法,以便于阅读。

这是代码:

public Deck()
{
    cards = new List<Card>();
    cardStack = new Stack<Card>();
    // cards list gets populated here
    foreach (Card card in cards)
    {
        cardStack.Push(card);
    }
}

public Card drawCard()
{
    return cardStack.Pop(); // This line is giving me an InvalidOperationException
}

谢谢!

So I have a constructor and a method that returns a card. I'm getting an InvalidOperationException for some reason. Anyone can think of anything?

The cards list is not empty, I just removed the card generation algorithm from the constructor here to make it easier to read.

Here's the code:

public Deck()
{
    cards = new List<Card>();
    cardStack = new Stack<Card>();
    // cards list gets populated here
    foreach (Card card in cards)
    {
        cardStack.Push(card);
    }
}

public Card drawCard()
{
    return cardStack.Pop(); // This line is giving me an InvalidOperationException
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏月而来 2024-11-08 17:15:45

当您调用 Pop() 时,cardStack 很可能为空。我建议你在弹出牌堆之前检查一下牌堆中的牌数,如果牌堆是空的,请采取合理的措施。

Chances are that cardStack is empty when you call Pop(). I suggest that you check the number of cards in the stack before you pop it and do something reasonable if the deck is empty.

赠佳期 2024-11-08 17:15:45

也许这是一个愚蠢的问题......但是您在弹出堆栈之前是否检查堆栈是否为空?根据 MS 文档,这是应该抛出此异常的唯一原因。 ..

Maybe this is a stupid question ... but are you checking the stack is non-empty before you pop it? According to the MS docs that's the only reason this exception should be thrown ...

尤怨 2024-11-08 17:15:45

查看该代码,实际上没有卡片被推送到cardStack 上(卡片最初是空的)。您收到异常是因为 C# 告诉您没有任何内容可以弹出。

Looking at that code, no cards actually get pushed onto cardStack (cards is initially empty). You're getting the exception because C# is telling you that there's nothing to pop.

预谋 2024-11-08 17:15:45

你里面没有卡!

您创建一个新的卡片列表,列表从空开始。然后你抓住它们全部(读:无)并将它们推入堆栈。你得到一个空堆栈。

然后您尝试弹出,但无法从空的空对象中弹出堆。 InvalidOperationException 和原因(“Stack为空”)就在文档中。

您需要通过添加 一些卡片。或者也许只是删除列表并直接初始化堆栈。

而且你还需要确保你没有弹出太多。如果您将所有卡片从堆栈中弹出,那么当您下一次弹出时,堆栈将是空的,您将遇到同样的问题。

You have no cards in there!

You create a new list of cards and that list starts empty. Then you grab them all (read: none) and push them into the stack. You get an empty stack.

And then you try popping, but you can't pop from an empty stack. The InvalidOperationException and the reason ("the Stack<T> is empty") are right there in documentation.

You need to initialize your list of cards by adding some cards. Or maybe just remove the list and initialize the stack directly.

And you also need to make sure you're not popping too much. If you pop all the cards out of the stack, when you pop next, the stack will be empty, and you'll run into the same problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文