Ref and Out Parameters 2

image_pdfimage_print

   


using System;
class Point
{
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    
    public void GetPoint(out int x, out int y)
    {
        x = this.x;
        y = this.y;
    }
    
    int x;
    int y;
}

public class RefOutParameters3
{
    public static void Main()
    {
        Point myPoint = new Point(10, 15);
        int x;
        int y;
        
        myPoint.GetPoint(out x, out y);
        Console.WriteLine("myPoint({0}, {1})", x, y);
    }
}

           
          


C# Ref and Out Parameters

image_pdfimage_print

   


using System;
class Point
{
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    
    public void GetPoint(ref int x, ref int y)
    {
        x = this.x;
        y = this.y;
    }
    
    int x;
    int y;
}

public class RefOutParameters2
{
    public static void Main()
    {
        Point myPoint = new Point(10, 15);
        int x = 0;
        int y = 0;
        
        myPoint.GetPoint(ref x, ref y);
        Console.WriteLine("myPoint({0}, {1})", x, y);
    }
}
           
          


Ref and Out Parameters: compiling error

image_pdfimage_print
   


// error
using System;
class Point
{
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    // get both values in one function call
    public void GetPoint(ref int x, ref int y)
    {
        x = this.x;
        y = this.y;
    }
    
    int x;
    int y;
}

public class RefOutParameters {
    public static void Main()
    {
        Point myPoint = new Point(10, 15);
        int x;
        int y;
        
        // illegal 
        myPoint.GetPoint(ref x, ref y);
        Console.WriteLine("myPoint({0}, {1})", x, y);
    }
}
           
          


Pass value by reference with read only value

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace PassByRef
 {

     public class Time
     {
         // private member variables
         private int Year;
         private int Month;
         private int Date;
         private int Hour;
         private int Minute;
         private int Second;

         // Property (read only)
         public int GetHour()
         {
             return Hour;
         }

         // public accessor methods
         public void DisplayCurrentTime()
         {
             System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
                 Month, Date, Year, Hour, Minute, Second);
         }

         // takes references to ints
         public void GetTime(ref int h, ref int m, ref int s)
         {
             h = Hour;
             m = Minute;
             s = Second;
         }

         // constructor
         public Time(System.DateTime dt)
         {

             Year = dt.Year;
             Month = dt.Month;
             Date = dt.Day;
             Hour = dt.Hour;
             Minute = dt.Minute;
             Second = dt.Second;
         }


     }

    public class TesterPassByRef
    {
       public void Run()
       {
           System.DateTime currentTime = System.DateTime.Now;
           Time t = new Time(currentTime);
           t.DisplayCurrentTime();

           int theHour = 0;
           int theMinute = 0;
           int theSecond = 0;

           // pass the ints by reference
           t.GetTime(ref theHour, ref theMinute, ref theSecond);

           System.Console.WriteLine("Current time: {0}:{1}:{2}",
               theHour, theMinute, theSecond);

       }

       static void Main()
       {
          TesterPassByRef t = new TesterPassByRef();
          t.Run();
       }
    }
 }

           
          


Pass value by reference

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace PassByRef
 {

     public class Time3
     {
         // private member variables
         private int Year;
         private int Month;
         private int Date;
         private int Hour;
         private int Minute;
         private int Second;

         // Property (read only)
         public int GetHour()
         {
             return Hour;
         }

         // public accessor methods
         public void DisplayCurrentTime()
         {
             System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
                 Month, Date, Year, Hour, Minute, Second);
         }

         public void GetTime(int h, int m, int s)
         {
             h = Hour;
             m = Minute;
             s = Second;
         }

         // constructor
         public Time3(System.DateTime dt)
         {

             Year = dt.Year;
             Month = dt.Month;
             Date = dt.Day;
             Hour = dt.Hour;
             Minute = dt.Minute;
             Second = dt.Second;
         }


     }

    public class PassByRefTester
    {
       public void Run()
       {
           System.DateTime currentTime = System.DateTime.Now;
           Time3 t = new Time3(currentTime);
           t.DisplayCurrentTime();

           int theHour = 0;
           int theMinute = 0;
           int theSecond = 0;
           t.GetTime(theHour, theMinute, theSecond);
           System.Console.WriteLine("Current time: {0}:{1}:{2}",
               theHour, theMinute, theSecond);

       }

       static void Main()
       {
          PassByRefTester t = new PassByRefTester();
          t.Run();
       }
    }
 }

           
          


Illustrates the use of out parameters

image_pdfimage_print

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example5_8.cs illustrates the use of out parameters
*/


// declare the MyMath class
class MyMath
{

  // the SinAndCos() method returns the sin and cos values for
  // a given angle (in radians)
  public void SinAndCos(double angle, out double sin, out double cos) {
    sin = System.Math.Sin(angle);
    cos = System.Math.Cos(angle);
  }

}


public class Example5_8
{

  public static void Main()
  {

    // declare and set the angle in radians
    double angle = System.Math.PI / 2;

    // declare the variables that will be used as out paramters
    double sin;
    double cos;

    // create a MyMath object
    MyMath myMath = new MyMath();

    // get the sin and cos values from the SinAndCos() method
    myMath.SinAndCos(angle, out sin, out cos);

    // display sin and cos
    System.Console.WriteLine("sin = " + sin + ", cos = " + cos);

  }

}

           
          


Passing parameters by value

image_pdfimage_print

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example5_6.cs illustrates passing parameters by value
*/


// declare the Swapper class
class Swapper
{

  // the Swap() method swaps parameters passed by value
  public void Swap(int x, int y)
  {

    // display the initial values
    System.Console.WriteLine("In Swap(): initial x = " + x +
      ", y = " + y);

    // swap x and y
    int temp = x;
    x = y;
    y = temp;

    // display the final values
    System.Console.WriteLine("In Swap(): final   x = " + x +
      ", y = " + y);
  }

}


public class Example5_6
{

  public static void Main()
  {

    // declare x and y (the variables whose values
    // are to be swapped)
    int x = 2;
    int y = 5;

    // display the initial values
    System.Console.WriteLine("In Main(): initial x = " + x +
      ", y = " + y);

    // create a Swapper object
    Swapper mySwapper = new Swapper();

    // swap the values in x and y
    mySwapper.Swap(x, y);

    // display the final values
    System.Console.WriteLine("In Main(): final   x = " + x +
      ", y = " + y);

  }

}