c# / .net
.NET 7, and the string literal I stopped escaping

.NET 7 was released on the eighth of November with C# 11. I've written before about .NET 6 making small services simpler, and this release carries on in the same direction. Three of the language additions would justify the upgrade even without the rest, I think.
Raw string literals
Every C# codebase I've worked in has a test with JSON in it, and that JSON is unreadable:
var expected = "{\"name\":\"Probe\",\"limits\":{\"memory\":\"256Mi\"}}";
In practice people stop checking whether it's correct and only check whether it
compiles. Verbatim strings help with the quotes and then break the moment the
content contains a " of its own.
C# 11 allows a literal delimited by three or more quotes, and that ends the escaping entirely:
var expected = """
{
"name": "Probe",
"limits": { "memory": "256Mi" }
}
""";
The closing delimiter sets the indentation. Whatever whitespace comes before it is stripped from every line, so the literal stays aligned with the code around it and the value doesn't carry that alignment. If the content itself contains three quotes, use four as the delimiter.
It looks like a typing convenience but it affects correctness too, because escaped JSON in a test stops being read, and then it stops being checked.
Required members
The other one I adopted straight away:
public sealed class ServiceOptions
{
public required string Name { get; init; }
public required Uri Endpoint { get; init; }
public int Retries { get; init; } = 3;
}
required means the compiler won't let an object initialiser omit that
property. An initialiser that leaves it out fails to compile. That's stronger
than a runtime check or a guard clause in a constructor, and stronger than a
nullable annotation, which only produces a warning.
This closes a specific and irritating gap. Object initialisers have always been the pleasant way to construct configuration-shaped objects, and also the way to forget a property without noticing. The previous answer was a constructor with eight parameters, which is worse to read and worse to extend. Now the initialiser syntax is the enforced one as well.
Adding a required property to an existing type is a source-breaking change for
everyone who constructs it. That is the intended behaviour, but you want to know
it before you add one to a public type.
List patterns
Pattern matching learned to look at sequences:
var result = segments switch
{
["health"] => Health(),
["v1", "members", var id] => Member(id),
["v1", "members", var id, .. var rest] => MemberSub(id, rest),
_ => NotFound(),
};
The .. is a slice pattern and it can bind. I find this clearer than the
equivalent chain of length checks and index lookups (the sort of code where
off-by-one bugs live).
I'd use it for parsing and dispatch and not much else. A switch over shapes is
readable at four cases. At fourteen it isn't.
The runtime side, and the number I would not quote
.NET 7 also brings a lot of performance work and continued investment in Native AOT, meaning compiling to a native executable with no JIT, so faster startup and a smaller footprint.
Native AOT is the one I'm most interested in and least ready to make claims about. It matters for the reasons I care about in containers. A process that starts faster scales out faster, and a smaller image is a cheaper deployment. But the published figures come from the workloads that were measured, and everything I have written about measuring a container's memory still applies. There are constraints as well. Reflection-heavy code and runtime code generation don't survive the trip, and that rules out a good deal of what a typical ASP.NET application does.
So, my summary of the release. The language changes are safe to adopt on Monday, and the runtime changes need an experiment with your own workload first. I'd have said the same about the last three .NET releases.
Tagged


