Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

426
Views
C# record ToString() causes stack overflow and stops debugging session with a strange error

I wrote a unit test and used the new C# record to store some data I needed for testing. The unit test run fine, but when I set a break point and moved the mouse over the record variable name, the debugging session ended and I got a strange looking error message.

To report the problem to Microsoft, I wrote a simple unit test, which demonstrates the problem, but doesn't make much sense otherwise:

#nullable enable
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CSharpRecordTest {

  record Twin(string Name) {
    public Twin? OtherTwin { get; set; }
  }

  [TestClass]
  public class UnitTest1 {
    [TestMethod]
    public void TestMethod1() {
      var twinA = new Twin("A");
      var twinB = new Twin("B");
      twinA.OtherTwin = twinB;
      twinB.OtherTwin = twinA;
      Assert.AreEqual(twinA, twinA.OtherTwin.OtherTwin);
    }
  }
}

The test runs fine, but when setting a break point at Assert.AreEqual( and moving the mouse over twinA, the debugging session stops and this error message gets shown:

enter image description here

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It took me a day or two to figure out what was happening, because the debugger did not let me see anything. I wrote a console application:

class Program {
  record Twin(string Name) {
    public Twin? OtherTwin { get; set; }
  }

  static void Main(string[] args) {
    var twinA = new Twin("A");
    var twinB = new Twin("B");
    twinA.OtherTwin = twinB;
    twinB.OtherTwin = twinA;
    Console.WriteLine(twinA);
  }
}

The console application also run into troubles, but at least it showed that there was a stack overrun and which line it caused:

Console.WriteLine(twinA);

The stack looked something like this (although more complicated):

twinA.OtherTwin.OtherTwin.OtherTwin.OtherTwin.OtherTwin.OtherTwin...OtherTwin.ToString()

The problem is that ToString(), which gets automatically written by the compiler, calls .ToString() again on every property.

If a record references itself or other records which then reference the first record, ToString() will fail and needs to get overriden together with Equals(), GetHashCode() and maybe more (I didn't try it).

Actually, when having a property which is not readonly, Equals() and GetHashCode() should be overriden, otherwise classes like Dictionary will not work.

For more details, see my article on CodeProject: C# 9 Record: Compiler Created ToString() Code can Lead to Stack Overflow and Worse

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!