Method Parameters
In C#, arguments can be passed to parameters either by value or by reference.
Pass by value means passing a copy of the variable to the method.
Pass by reference means passing access to the variable to the method.
A variable of a reference type contains a reference to its data.
A variable of a value type contains its data directly.
Reference parameters
ref
parameter modifier
ref
parameter modifierThe
ref
keyword causes arguments to be passed by reference, not by value.The argument for a
ref
parameter must be definitely assigned. The called method may reassign that parameter.
out
parameter modifier
out
parameter modifierThe
out
keyword causes arguments to be passed by reference.The argument for an
out
parameter needn't be definitely assigned. The called method must assign the parameter.
in
parameter modifier
in
parameter modifierThe
in
keyword causes arguments to be passed by reference.The argument for an
in
parameter must be definitely assigned. The called method can't reassign that parameter.
Parameters declared for a method without in
, ref
or out
, are passed to the called method by value.
Last updated