
Ref and out change the behavior of method parameters. Sometimes, you want the actual value of a variable to be copied as the parameter. Other times, you want a reference to the original variable. These modifiers change how definite assignment analysis is handled. They illustrate the difference between arguments and parameters.
Ref Out
In addition to the Main method, this program introduces three methods: the Example1 method, which uses the default parameter passing technique; the Example2 method, which uses the ref modifier; and the Example3 method, which employs the out modifier. We pass the local int val to each of these three methods and examine the results.
This C# example code shows parameters with the ref and out modifiers.
Program that demonstrates parameter passing [C#]
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;
}
}
Output
0
2
3
Effect of Example1. The Example1 method uses value-passing semantics for its declaration. Therefore, when it changes the value of its parameter int, that only affects the local state of the method. The int in the Main method retains its current value when Example1 returns.
Effect of Example2. The Example2 method uses the ref modifier for its int parameter. This informs the compiler that a reference to the actual variable (int val) is to be passed to Example2. You can also see that the 'ref' keyword is used in the calling syntax in Main(). When Example2 sets its parameter to 2, this is reflected in the Main method.

Effect of Example3. Finally, the Example3 method uses the out modifier on its parameter, which works the same way as the ref modifier except it has some compile-time checking differences. These involve the definite assignment rules in the C# language (see below). Example3 sets its parameter to 3, and this is reflected in the calling location.

So what is the difference between ref and out in the C# language? They both indicate that the parameter is being passed by reference. The difference is in the compiler's application of the definite assignment analysis step. When the out modifier is used, the compiler demands that the parameter be "definitely assigned" before any exit. There is no such restriction with the ref modifier.
When a method uses the out modifier, you can be sure that after you invoke it, your variable is definitely assigned. This means you can write code that does not assign the variable before calling an out method, which leads to clearer code in many cases.

It can be confusing when learning the C# language what the difference between a value and a reference is. Actually, there is no difference between references and values at some levels of compilation. References are values; each is represented by a series of bytes that are stored together.
However, a reference is a value that points to a location elsewhere in memory; a value stores an immediate value such as a number. The compiler and language enforce rules on what you can do with references, to avoid type safety issues and ensure your program doesn't do anything horrible to the system.

Arguments are values such as integers or references that are passed to a specific method call. You can call a method with many different arguments, as many times as you wish. The name of the argument variables (if any) does not affect the behavior of the method.
Program 1 [C#]
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);
}
}
Output
5
SamParameters are also called formal parameters and they are found in the method itself. The names they use are not affected by the argument names. When the arguments are passed to the method, they are received as formal parameters with the indicated names.
Program 2 [C#]
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);
}
}
Output
5
Sam
In the Structure and Interpretation of Computer Programs, procedures are described as black-box abstractions. The point of procedures is do a specific task and not be susceptible to any interference. Procedures are concerned with tasks, not with how those tasks are done.
Formal parameters are important because their names do not matter. This means that they will not be affected by other names in the program, making procedures become good black-box abstractions as desired.
Bound variables and free variables. Formal parameters are bound variables, which means they have a specific meaning only within that function. Free variables, on the other hand, could conflict and create ambiguous meanings in programs. (See pages 26-29.)
Review: Arguments are specific values that are passed to a method; parameters are the variables where those values are received. This disconnect provides a profound level of abstraction, which is useful for developing complex yet understandable computer programs.

The C# language has strict rules regarding the passing of parameters by value and by reference. The default is to pass all parameters by value, even if those parameters are themselves references. The out modifier is often preferred to the ref modifier because it ensures that you can avoid assigning the variable before passing it as an argument.
Method Tips