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);

  }

}

           
          


Passing parameters by reference

image_pdfimage_print

   

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

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example5_7.cs illustrates passing parameters by reference
*/


// declare the Swapper class
class Swapper
{

  // the Swap() method swaps parameters passed by reference
  public void Swap(ref int x, ref 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_7
{

  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, passing a reference to the Swap() method
    mySwapper.Swap(ref x, ref y);

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

  }

}

           
          


Parameter demo

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

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

 namespace ParamsDemo
 {
    public class TesterParamsDemo
    {
       public void Run()
       {
           int a = 5;
           int b = 6;
           int c = 7;
           Console.WriteLine("Calling with three integers");
           DisplayVals(a,b,c);

           Console.WriteLine("
Calling with four integers");
           DisplayVals(5,6,7,8);

           Console.WriteLine("
calling with an array of four integers");
           int [] explicitArray = new int[4] {5,6,7,8};
           DisplayVals(explicitArray);
       }

        // takes a variable number of integers
        public void DisplayVals(params int[] intVals)
        {
            foreach (int i in intVals)
            {
                Console.WriteLine("DisplayVals {0}",i);
            }
        }

       [STAThread]
       static void Main()
       {
          TesterParamsDemo t = new TesterParamsDemo();
          t.Run();
       }
    }
 }

           
          


Use regular parameter with a params parameter

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use regular parameter with a params parameter. 
 
using System; 
 
class MyClass { 
  public void showArgs(string msg, params int[] nums) { 
    Console.Write(msg + ": "); 
 
    foreach(int i in nums) 
      Console.Write(i + " "); 
 
    Console.WriteLine(); 
  } 
} 
 
public class ParamsDemo2 { 
  public static void Main() { 
    MyClass ob = new MyClass(); 
 
    ob.showArgs("Here are some integers",  
                1, 2, 3, 4, 5); 
 
    ob.showArgs("Here are two more",  
                17, 20); 
 
  } 
}

           
          


Demonstrate params

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate params.

using System;

class Min {
public int minVal(params int[] nums) {
int m;

if(nums.Length == 0) {
Console.WriteLine(“Error: no arguments.”);
return 0;
}

m = nums[0];
for(int i=1; i < nums.Length; i++) if(nums[i] < m) m = nums[i]; return m; } } public class ParamsDemo { public static void Main() { Min ob = new Min(); int min; int a = 10, b = 20; // call with two values min = ob.minVal(a, b); Console.WriteLine("Minimum is " + min); // call with 3 values min = ob.minVal(a, b, -1); Console.WriteLine("Minimum is " + min); // call with 5 values min = ob.minVal(18, 23, 3, 14, 25); Console.WriteLine("Minimum is " + min); // can call with an int array, too int[] args = { 45, 67, 34, 9, 112, 8 }; min = ob.minVal(args); Console.WriteLine("Minimum is " + min); } } [/csharp]