参数更改原始值

时间:2015-07-02 11:39:52

标签: c# list reference

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

这是我的方法

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

事情就是当我检查我的“如果”时,我得到了第一个方法“RoyalFlushJokerHelper”,我在那里从删除列表中删除了所有我的5个项目。

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

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

1 个答案:

答案 0 :(得分:1)

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

  • 数字数据类型
  • 布尔值,字符和日期
  • structŠ
  • enum s
  • Long,Byte,UShort,UInteger或ULong

所有其他对象都是引用类型,当您将它们传递给函数时,将其作为参考传递
如果您想在不影响List的情况下更改public bool RoyalFlushJokerHelper(object honoursOnTheScreen, List<Honours> honours) { var honoursCopy = honours.Clone(); // work with honoursCopy } ,可以先将其克隆:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shuffleon" android:state_checked="true" />
    <item android:drawable="@drawable/shuffleoff" android:state_selected="false" />
</selector>

请阅读有关值类型和参考类型的一些信息 例如,请查看MSDN article "Value Types and Reference Types"

相关问题