Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

C# 9 Record: Compiler Created ToString() Code can Lead to Stack Overflow and Worse

Rate me:
Please Sign up or sign in to vote.
4.97/5 (18 votes)
26 Apr 2021CPOL6 min read 18.8K   5  
If a record declaration creates a cyclical reference, the compiler generated ToString() causes a stack overflow.
When a record declaration has a property referencing itself, a reference cycle is generated which causes the compiler to write a ToString() method which will cause a stack overflow. The C# language designers say this is by design and refuse to issue a compiler warning, while most merely mortals would consider this a bug. Even worse, the debugging session might stop abruptly and the developer has no way to figure out the problem, because the stack trace is no longer available. Be warned when you design your own records.

Views

Daily Counts

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
Retired SW Developer from Switzerland living in Singapore

Interested in WPF projects.

Comments and Discussions