Reputation: 51917
I've just been told that readonly ref readonly
is now valid C#, I can clearly see its use as an interview question, but otherwise when is it of real-life use?
Upvotes: 5
Views: 516
Reputation: 32068
When you have a mutable struct in which you want to have a field ref readonly
(a value you get by reference that cannot be modified by the caller) that does not mutate the struct.
You can find quite a good explanation here: "Readonly Ref vs Ref Readonly in C# Struct"
Now, as to "real-life" use cases... that's probably for performance-sensitive code, where copying the struct is too expensive.
Also, it's not really "new", it's possible to do this since C# 7.2: Microsoft Docs | Readonly references
Upvotes: 8