Share via


An example of mutable-struct-pain

Sean asks

How does a mutable struct get me into trouble in my C# code?

I guess that's a fair question, so I went look for some examples. Here's one that a co-worker (Renaud) shared:

Actual example that bit me. This used to be a struct (because I thought having only 1 member made it a good candidate), but then passing it by value into various parsing methods was just "resetting" "_ptr" to its previous value. I decided passing by ref would be horrible and prone to error (it's very easy to forget to pass by ref).

public class MemoryMarshaler

{

private IntPtr _ptr;

public MemoryMarshaler(IntPtr ptr)

{

_ptr = ptr;

}

public void Advance(int cbLength)

{

long p = _ptr.ToInt64();

p += cbLength;

_ptr = (IntPtr)p;

}

public IntPtr Ptr

{

get

{

return _ptr;

}

set

{

_ptr = value;

}

}

(... there were more members here...)

}

Example of usage:

void ParseSthg(MemoryMarshaler m)

{

(...)

m.Advance(3);

(...)

}

Comments

  • Anonymous
    June 14, 2004
    Jay,

    I see the point, but I would argue this is by design. Value types require value type symantics when passed around. To me, this isn't a problem with a[n interally] mutable struct. Reference type symantics were called for in this situation, so changing to a class definition is the correct approach.

    If the method was written as

    void ParseSthg(int x)
    {
    (...)
    x++;
    (...)
    }

    would the complaint be made that x wasn't modified coming out of call?

    (My apologies for posting this as late as I am. I'm still catching up on my reading.)
  • Anonymous
    June 14, 2004
    Ron: no need to apologize; it's my fault for posting too fast :-)

    I wonder if there's some way to allow locals to by mutated, but not parameters. Maybe that would help all around.
  • Anonymous
    June 14, 2004
    It could be reasoned that by value parameters should be immutable. For the sake of consistency, I would apply the behavior to all by value parameters. The only catch would be in code that uses such a parameter as a bit bucket (instead of allocating a new local variable).

    I wonder how often this practice is used and whether such a change would be too much for legacy code.
  • Anonymous
    November 20, 2007
    PingBack from http://msdnrss.thecoderblogs.com/2007/11/20/enforcing-immutability-in-code/