Passing a (Generic) Stack by Reference

  • Thread starter Thread starter Mahmoud Al-Qudsi
  • Start date Start date
M

Mahmoud Al-Qudsi

Hi all,

I'm working on a C# project, and at some point I'm needing to pass the
same stack to multiple functions.

I found out the hard way that when I poss a stack by value it doesn't
actually use a copy constructor to move the contents over to a new
stack - the original stack is still modified by changes to the stack
that was passed by value.

Looking at the MSDN page (http://msdn2.microsoft.com/en-us/library/
3278tedw.aspx) I don't see anything that explicitly says it's (un)safe
to pass by reference.

My question: is there an easy way to copy this stack to a new one
without creating my own "stack copy" function?
Also: In the future, how can I tell if it is safe to pass *and modify*
a stack to a new function?

Thanks,
 
Mahmoud Al-Qudsi said:
I'm working on a C# project, and at some point I'm needing to pass the
same stack to multiple functions.

I found out the hard way that when I poss a stack by value it doesn't
actually use a copy constructor to move the contents over to a new
stack - the original stack is still modified by changes to the stack
that was passed by value.

That's because it's a reference type.

See http://pobox.com/~skeet/csharp/parameters.html
Looking at the MSDN page (http://msdn2.microsoft.com/en-us/library/
3278tedw.aspx) I don't see anything that explicitly says it's (un)safe
to pass by reference.

You need to be clear about the difference between passing a reference
by value and passing a value by reference.
My question: is there an easy way to copy this stack to a new one
without creating my own "stack copy" function?

new Stack said:
Also: In the future, how can I tell if it is safe to pass *and modify*
a stack to a new function?

What kind of "safety" are you concerned about?
 
That's because it's a reference type.

Seehttp://pobox.com/~skeet/csharp/parameters.html


You need to be clear about the difference between passing a reference
by value and passing a value by reference.




What kind of "safety" are you concerned about?

Thanks for your reply.

I'm clear on what (and how) it is happening (thanks to painful years
of C++ coding), just not sure how to tell beforehand if it would
happen or not.
Casting the old stack to an IEnumerable and putting that in the new
stack's constructor like you suggested has done the trick - thanks.

I'm already using a thread-safe wrapper for thread-safety, the only
other concern I had was the preservation of data when sending it to
another function - and now that is working fine too.

Thanks :)
 
It should be noted that the copy of the stack that you make is a shallow
copy. If the elements in the stack are reference types, and you manipulate
elements in the new stack, then those changes are going to be reflected in
the original stack.
 
Mahmoud Al-Qudsi said:
I'm clear on what (and how) it is happening (thanks to painful years
of C++ coding), just not sure how to tell beforehand if it would
happen or not.

You just need to know whether you're dealing with a reference type (a
class/delegate/array/interface) or a value type (a struct/enum).
 
Back
Top