When providing a parameter to a method, we can either pass a value to it or the variable's reference. Depending upon the method used, the passed parameter can have different values when the method ends. Understanding the difference between the two helps us know exactly what a method is doing to pass data and predict what to expect at the end of the method.
Generally, when a parameter is passed without
in keyword, the parameter is passed as value.
Pass By Value
When a class or variable is created, data is created in memory along with a reference that points to the created data. Passing by value means that the data is passed to the method.
Here, we can see any change made by the method to the parameter directly changes the passed object. Also, notice that after assigning
userInfo to a new class in the
ChangeName method, the passed value
InfoUpdater still retains its old value. This is because the pass by value doesn't change the reference of the passed parameter.
Pass By Reference
To pass the reference of a variable, the
ref keyword can be used before the passed variable. Method signature should also contain the
ref keyword before the parameter to take it as a reference. When it is used, the object's reference is passed instead of the value. Any assignment or change of reference inside the method is reflected in the passed parameter.
In this snippet, notice that assigning a new class to
userInfo inside the
ChangeName method also changes the object referenced by the passed reference –
InfoUpdater constructor. So, the object referenced by
user1 is changed at the end of the method.
Other Ways of Passing By Reference
Reference can be passed to a method using
out keyword instead of
ref. These two methods add some restrictions on how references can be modified.
out keyword makes sure the passed reference is assigned inside it before exiting the method as the reference passed should always be modified. The parameter passed is usually uninitialized or null as it will be assigned inside the method. It can be used to get multiple results from a single method. Multiple
out parameters can be passed to a method. They will get assigned inside the method, thus resulting in those parameters when the method exits.
in keyword makes the passed parameter unassignable. The reference of the passed object can never be modified, but if an object is passed, the object's property can be modified.
The main difference between
out is that parameters passed with
ref can be modified, parameters passed with
in cannot be modified, and parameters passed with
out must be modified.
While using these, keep in mind that all three keywords seem to have the same signature. It means the method cannot be overloaded by swapping these keywords with each other. However, a method without reference type (pass by value) is seen as having a different signature, so they can be overloaded.
Understanding parameter types in methods help us track and understand how the method uses the passed parameter. This also helps us restrict passed parameters to ensure that the method uses parameters in an intended way.
Thanks for reading!