Search⌘ K
AI Features

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.

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 out parameter: Think of these as being out-only. out parameters 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 ref parameter: Think of these as being in and out. Like out parameters, ref parameters 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. ...