79211617

Date: 2024-11-21 14:35:16
Score: 3
Natty:
Report link

If you put this code inside a test class... and set two breakpoints at the arrange and assert calls, you will be able to use the diagnotics window in VS2022 to inspect the details

Microsoft documentation for VS2022

struct ExampleStruct
{
    public int ID;
    public List<ExampleStruct> children;
}

public class StackOverflowStructQ
{
    [Fact]
    public void Method_Condition_Expectation()
    {
        // Arrange
        ExampleStruct es = new() { ID = 1, children = [] };

        // Act

        // Assert
        Assert.NotNull(es.children);
    }
}

The when the first breakpoint is hit - "take a snapshot" from the Diagnostics window.

You should be able to drill down and see "Referenced Objects". This means the reference is stored in the struct, but the object it references is stored on the heap.

So try and keep your structs lightweight. If you need to reference objects which will ultimately reside on the heap, probably best to use a class?

Reasons:
  • Blacklisted phrase (1): StackOverflow
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (1):
Posted by: PureAlpha