Parameter out and reference
using System;
class ReferenceAndOutputParamtersTest
{
static void Main( string[] args )
{
int y = 5;
int z;
Console.WriteLine( "Original value of y: {0}", y );
Console.WriteLine( "Original value of z: uninitialized\n" );
SquareRef( ref y ); // must use keyword ref
SquareOut( out z ); // must use keyword out
Console.WriteLine( "Value of y after SquareRef: {0}", y );
Console.WriteLine( "Value of z after SquareOut: {0}\n", z );
Square( y );
Square( z );
Console.WriteLine( "Value of y after Square: {0}", y );
Console.WriteLine( "Value of z after Square: {0}", z );
}
static void SquareRef( ref int x )
{
x = x * x;
}
static void SquareOut( out int x )
{
x = 6;
x = x * x;
}
static void Square( int x )
{
x = x * x;
}
}
Related examples in the same category