Controlling How Parameters Are Passed
Explore how to pass parameters in C# methods using by value, ref, and out keywords. Understand differences, practical examples, and simplifications introduced in C# 7.0 for better control over variable states when building your own types.
We'll cover the following...
When a parameter is passed into a method, it can be passed in one of three ways:
By value (the default): Think of these as in-only.
As an
outparameter: Think of these as being out-only.outparameters cannot have a default value assigned in the parameter declaration and cannot be left uninitialized. They must be set inside the method, or the compiler will give an error.By reference as a
refparameter: Think of these as being in and out. Likeoutparameters,refparameters also cannot have default values, but since they can already be set outside the method, they do not need to be set inside the method. ...