C# 7.2 introduced in
parameters for methods. I understand how they work:
int MyMethod(in int i) {
i += 1; // illegal, i is read-only
return i + 1;
}
I undestand that these parameters are passed by reference (like ref
and out
).
What I don't understand is the purpose of the in
parameters. What do I gain by using them?
I understand the purpose of out
and ref
parameters, it is quite useful to be able to assign values to variables in the calling scope, but that function is non-existant with the in
parameters.
"in" Can be used to pass a variable as a read-only reference (value type and reference type both). Reason to pass read-only reference is that, if I pass a value type variable without reference, then each time a new copy will be created. So, it will take extra memory and performance will be slower.