Home
Map
Parameter, ref and out ExamplesUse parameters with the ref and out modifiers. Compare ref and out.
C#
This page was last reviewed on Feb 19, 2023.
Parameters. In C# programs, ref and out change the behavior of method parameters. Sometimes we want the actual value of a variable to be copied as the parameter.
Other times we want a reference. These modifiers (ref and out) affect definite assignment analysis. They are governed by specific rules.
ref
out
An example. This program introduces 3 methods. Example1 uses the default parameter passing technique. Example2 uses the ref modifier. And Example3 employs the out modifier.
Next We pass the local int val to each of these 3 methods and examine the results.
int, uint
Note Example1 uses value-passing semantics. If it assigns the parameter, it only affects the local state of the method.
And Example2 uses ref. When this method sets its parameter to 2, this is reflected in the calling location (Main).
Finally Example3 uses out on its parameter. It sets its parameter to 3—this is reflected in the calling location.
using System; class Program { static void Main() { int val = 0; Example1(val); Console.WriteLine(val); // Still 0. Example2(ref val); Console.WriteLine(val); // Now 2. Example3(out val); Console.WriteLine(val); // Now 3. } static void Example1(int value) { value = 1; } static void Example2(ref int value) { value = 2; } static void Example3(out int value) { value = 3; } }
0 2 3
Arguments, parameters. Arguments are values that are passed to a specific method call. You can call a method with many different arguments, as many times as you wish.
Detail The name of the argument variables (if any) does not affect the behavior of the method.
using System; class Program { static void Main() { // Argument = 5 // Argument = Sam Perls(5, "Sam"); } static void Perls(int id, string name) { Console.WriteLine(id); Console.WriteLine(name); } }
5 Sam
Parameters, continued. Formal parameters are found in the method itself. The names they use are not affected by the argument names.
And When the arguments are passed to the method, they are received as formal parameters.
using System; class Program { static void Main() { Perls(5, "Sam"); } static void Perls(int id, string name) { // Parameter = id // Parameter = name Console.WriteLine(id); Console.WriteLine(name); } }
5 Sam
Ref, out. What is the difference between ref and out? The difference is in the compiler's application of the definite assignment analysis step.
Important The compiler demands that an out parameter be "definitely assigned" before any exit. There is no such restriction with the ref.
Detail When a method uses the out modifier, you can be sure that after you invoke it, your variable is definitely assigned.
And This means you can write code that does not assign the variable before calling an out method.
References. References are values. They are both represented by a series of bytes that are stored together. But references point to somewhere in memory.
Further The compiler enforces rules on references. These avoid type safety issues and ensure your program doesn't do anything unwanted.
A summary. The C# language has strict rules regarding the passing of parameters by value and by reference. The default is to pass all parameters (even references) by value.
The best choice. Out is often preferred to ref. It ensures that you can avoid assigning the variable before passing it as an argument.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 19, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.