in Parameters · TL;DR
1 min readTL;DR
An in parameter (C# 7.2) passes an argument by read-only reference: the method receives the address of the caller's variable (8 bytes on 64-bit) instead of a copy of its value, and the compiler forbids the method from modifying it. Its purpose is performance for large structs: passing a 64-byte struct by value copies 64 bytes on every call; passing it in copies a pointer. It sits beside ref (read-write reference, must be initialized), out (write-only reference, must be assigned by the callee) and ref readonly parameters (C# 12, like in but requiring a variable). Traps: in on a non-readonly struct can make the compiler take hidden defensive copies when you call its members, erasing the benefit (use readonly struct / readonly record struct); for small structs and primitives in is usually slower (an extra indirection); the value can change underneath you through aliasing; and in parameters cannot be used in async methods, iterators or captured by lambdas.