Illustrates the use of out parameters : Parameters Passing « Language Basics « C# / C Sharp






Illustrates the use of out parameters

Illustrates the use of out parameters
/*
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);

  }

}

           
       








Related examples in the same category

1.Parameter out and referenceParameter out and reference
2.Passing Parameters By Value and By RefPassing Parameters By Value and By Ref
3.Objects can be passed to methodsObjects can be passed to methods
4.Simple types are passed by valueSimple types are passed by value
5.Objects are passed by referenceObjects are passed by reference
6.Use ref to pass a value type by referenceUse ref to pass a value type by reference
7.Swap two valuesSwap two values
8.Use outUse out
9.Use two out parametersUse two out parameters
10.Swap two referencesSwap two references
11.Demonstrate paramsDemonstrate params
12.Use regular parameter with a params parameterUse regular parameter with a params parameter
13.Parameter demoParameter demo
14.Passing parameters by referencePassing parameters by reference
15.Passing parameters by valuePassing parameters by value
16.Pass value by referencePass value by reference
17.Pass value by reference with read only valuePass value by reference with read only value
18.Ref and Out Parameters: compiling error
19.C# Ref and Out ParametersC# Ref and Out Parameters
20.Ref and Out Parameters 2Ref and Out Parameters 2