0

I guess it is stupid question but if You can take a look

Here is my method

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

The thing is when I am checking my "If" I get to the first method " RoyalFlushJokerHelper" and there I am deleting all my 5 items from flush list.

Then the problem is when i step into ContainAllItems method my flush list is empty.

I am not passing it by reference so why does the first method changes my original list ?

1
  • What makes you believe you do not pass it via reference ? Commented Jul 2, 2015 at 11:43

1 Answer 1

1

In C# only these objects are value-types:

  • Numeric data types
  • Boolean, Char, and Date
  • structs
  • enums
  • Long, Byte, UShort, UInteger, or ULong

All other objects are reference-types and when you pass them to a function, you pass it as reference.
If you want to change your List without affecting it, you can clone it before:

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

Please, read some information about values types and reference types.
For example, take a look at MSDN article "Value Types and Reference Types".

Sign up to request clarification or add additional context in comments.

2 Comments

class objects are reference-type is not true because premitive type also derived from that type only. its parent of all type in .net
@PranayRana I didn't mean objects. I meant class objects - the objects which are classes. Probably, I named it wrong. Anyway, I have fixed an answer. Please, if you don't mind, review it and let me know if something's wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.