Ref-Readonly and In
05/01/2025
The current version of C# (C# 13 at the time of writing) has in
and ref
keywords. In this post I investigate using these keywords as parameter modifiers. Specifically, the difference between ref readonly
and the in
parameter modifier.
Both parameter modifiers are well-suited for methods that have large struct
parameters. Regular struct
parameters follow copy-by-value semantics. Larger struct copies incur a performance penalty as the larger the struct is the more data has to be copied. I investigated the cost of copies in a post about inline arrays. One approach to address this problem is by passing a reference to the struct
parameters.
In modifier
An author of a method could use the in
parameter modifier for value type parameters. In the following example as RegularStruct
is passed to a method named PassByIn
, with the in
modifier: