参数改变原值

Parameter changes original value

我想这是个愚蠢的问题,但如果你能看一看

这是我的方法

 public Tuple CheckRoyalFlush()
        {
            List<Honours> flush = new List<Honours>()
            {
                Honours.Ace,
                Honours.King,
                Honours.Queen,
                Honours.Jack,
                Honours.Ten
            };

            if (RoyalFlushJokerHelper(honoursOnTheScreen, flush) || ContainsAllItems(honoursOnTheScreen, flush))
            {
                Suits suit = cardsOnTheScreen.ElementAt(0).GetSuit();
                foreach (Card card in cardsOnTheScreen.Skip(1))
                {
                    if (card.GetSuit() != suit)
                    {
                        if (card.GetHonour() == Honours.Joker)
                            continue;
                        else
                            return new Tuple(false, null);
                    }
                }
                return new Tuple(true, new List<int> { 0, 1, 2, 3, 4 });
            }

问题是,当我检查我的 "If" 时,我进入了第一个方法“RoyalFlushJokerHelper”,然后我从刷新列表中删除了我的所有 5 个项目。

那么问题是当我进入 ContainAllItems 方法时,我的刷新列表是空的。

我没有通过引用传递它,为什么第一种方法会更改我的原始列表?

在 C# 中,只有这些对象是值类型:

  • 数值数据类型
  • 布尔、字符和日期
  • structs
  • enums
  • 长整型、字节、UShort、UInteger 或 ULong

所有其他对象都是引用类型,当您将它们传递给函数时,您将它作为引用传递
如果你想改变你的List而不影响它,你可以在之前克隆它:

public bool RoyalFlushJokerHelper(object honoursOnTheScreen, List<Honours> honours)
{
    var honoursCopy = honours.Clone();
    // work with honoursCopy
}

请阅读一些关于值类型和引用类型的信息。
例如,看看 MSDN article "Value Types and Reference Types".