Home > OS >  CS8852 Cannot edit value of init property in VS Debugger
CS8852 Cannot edit value of init property in VS Debugger

Time:10-21

I have a property defined as public bool? Enable{get; init} in a record. After the record is created, I broke into VS debugger and tried editing its value. It gives the following error when doing so:

CS8852: Init-only property or indexer can only be assigned in object initializer...

Isnt the debugger supposed to edit in memory ? Why is this not the case.

CodePudding user response:

We can see the definition of init:

In C# 9 and later, the init keyword defines an accessor method in a property or indexer. An init-only setter assigns a value to the property or the indexer element only during object construction. This enforces immutability, so that once the object is initialized, it can't be changed again.

So it will report an error if try to modify its value when debugging.

This link can give more information. Hope it can help you.

  • Related