Using the ref keyword
In C-Sharp, it’s possible to pass arguments as reference, instead of values.
- The traditional way to pass an argument to a method parameter would result in a copy of the argument to be used inside the method. This is passing the parameter as a value (which will create a copy of it).
- To pass a parameter as reference, use the ref keyword to declare the method and when calling it. This will reference the memory location of the variable. All changes made inside the code block will persist after calling the method.
Using the out keyword
Similar to ref, out also passes a variable by reference. Using out, the variable doesn’t need to be initialized before the calling the method, but must be before exiting it. Using out, it’s possible to return multiple values from a single method.