Get the latest tech news

Unexpected inconsistency in records


Unexpected inconsistency in records The other day, I was trying to figure out a bug in my code, and it turned out to be a misunderstanding on my part as to how C# records work. It’s entirely …

If we change the code above (while leaving the record itself the same) to create the second Number using the with operator instead of by calling the constructor, the output becomes incorrect: In theory, at least for any records being used within the same solution in which they’re declared (which is everything for my election site) it should be feasible to write a Roslyn analyzer which: Now maybe the Roslyn compiler could start raising warnings (option 2) so that I don’t have to write an analyzer – and maybe there are alternatives that could be added to C# for later versions (ideally giving more flexibility for initialization within records in general, e.g. a specially named member that is invoked when the instance is “ready” and which can still write to read-only properties)… but I’m probably not going to start creating a proposal for that without explicit encouragement to do so.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of records

records

Photo of coding blog

coding blog

Photo of Jon Skeet

Jon Skeet

Related news:

News photo

More than 3 million records, 12TB of data exposed in major app builder breach

News photo

Mysterious Database of 184 Million Records Exposes Vast Array of Login Credentials

News photo

Mysterious Database of 184 Million Records Exposes Vast Array of Login Credentials