record types .ToString() doesn't detect circular references #4390
Replies: 3 comments
-
THis is by design. There is no cycle detection in records in any operation (hashing, equality, stringifying). If you want to support cyclic records, you need to provide the code to determine what happens here. |
Beta Was this translation helpful? Give feedback.
-
An additional point: record types are immutable by default. It is certainly possible, as you can see, to make mutable record types, and when you do so you need to be careful of things like this. This is again by design. |
Beta Was this translation helpful? Give feedback.
-
In response to your "shouldn't the compiler prevent this" comment, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Description
I ran into StackoverflowExceptions caused by infinite loops of ToString() calls along circular references of record types. This is surprising, because to my understanding, record types should be immutable - so in theory circular references should not be possible. (I observed this with records used as EF entities, and the navigation properties often create circular references). I wondered how this is possible on one hand (this may be my bad), and on the other, I would suggest putting in place a circular reference detection in the generated ToString() code. In particular, because the VS debugger apparently evaluates all local variables .ToString() when hitting a breakpoint.
This can easily be reproduced:
Configuration
.net 5.0.102 / Windows 10 / x64
Beta Was this translation helpful? Give feedback.
All reactions