Re: Structures not behaving as value types
Jon Skeet <[email protected]> Mon, 25 Feb 2008 19:05:09 +0000
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <[email protected]> |
Richard Kucia wrote: <snip> > Public Shared Operator <<(ByVal Operand As strFieldMask, ByVal Bits As Integer) As strFieldMask > Dim Result As strFieldMask > Dim BitCrossing As Integer > ' Brute force method: shift one bit at a time > Result = Operand The last quoted line above is the guiltiest line in this particular case - by copying the operand as the result, the reference is copied for the array, so changes to the result (i.e. the shifting itself) also affect the operand. However, what you're seeing is primarily due to breaking another more general rule: don't make structures mutable. It's very fragile, as you've discovered - and the behaviour you've seen isn't the only unexpected result you might encounter. Embedding a reference type within a value type is *rarely* a good idea, but it's not too bad if either the reference type itself is immutable, or the reference will only ever be a private one and you only use it in an immutable way. Jon =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com