#273 – Parameter Modifier Summary
March 17, 2011 1 Comment
Here’s a summary of the different parameter modifiers and how the behavior changes for each, when using them with value-typed and reference-typed variables.
- No modifier – value types
- Copy of value passed to method
- Method can read value
- No modifier – reference types
- Reference to object passed to method
- Method can read/write object contents
- ref modifier – value types
- Reference to variable passed to method
- Method can read/write variable
- ref modifier – reference types
- Reference to object reference passed to method
- Method can read/write object contents
- Method can change reference to point to new object
- out modifier – value types
- Reference to variable passed to method
- Method must write new value to variable
- out modifier – reference types
- Reference to object reference passed to method
- Method must change reference to point to new object
Additionally, the “in” modifier was added in C# 7.2, which is by-reference but read-only. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier