Reference, output and value parameters in C#

using System;
 
class ReferenceAndOutputParameters
{
   // call methods with reference, output and value parameters
   public static void Main( string[] args )
   {
      int y = 5; // initialize y to 5
      int z; // declares z, but does not initialize it
 
      // display original values of y and z
      Console.WriteLine( "Original value of y: {0}", y );
      Console.WriteLine( "Original value of z: uninitialized\n" );
 
      // pass y and z by reference
      SquareRef( ref y ); // must use keyword ref
      SquareOut( out z ); // must use keyword out
 
      // display values of y and z after they are modified by 
      // methods SquareRef and SquareOut, respectively
      Console.WriteLine( "Value of y after SquareRef: {0}", y );
      Console.WriteLine( "Value of z after SquareOut: {0}\n", z );
 
      // pass y and z by value
      Square( y );
      Square( z );
 
      // display values of y and z after they are passed to method Square
      // to demonstrate arguments passed by value are not modified
      Console.WriteLine( "Value of y after Square: {0}", y );
      Console.WriteLine( "Value of z after Square: {0}", z );
   } // end Main
 
   // uses reference parameter x to modify caller's variable
   static void SquareRef( ref int x )
   {
      x = x * x; // squares value of caller's variable
   } // end method SquareRef
 
   // uses output parameter x to assign a value 
   // to an uninitialized variable
   static void SquareOut( out int x )
   {
      x = 6; // assigns a value to caller's variable
      x = x * x; // squares value of caller's variable
   } // end method SquareOut
 
   // parameter x receives a copy of the value passed as an argument,
   // so this method cannot modify the caller's variable
   static void Square( int x )
   {
      x = x * x;
   } // end method Square
} // end class ReferenceAndOutputParameters

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.