/* 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); } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Passing parameters by value
/* 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
/* 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
/* 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
/* 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
/*
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]
Swap two references
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Swap two references. using System; class RefSwap { int a, b; public RefSwap(int i, int j) { a = i; b = j; } public void show() { Console.WriteLine("a: {0}, b: {1}", a, b); } // This method now changes its arguments. public void swap(ref RefSwap ob1, ref RefSwap ob2) { RefSwap t; t = ob1; ob1 = ob2; ob2 = t; } } public class RefSwapDemo { public static void Main() { RefSwap x = new RefSwap(1, 2); RefSwap y = new RefSwap(3, 4); Console.Write("x before call: "); x.show(); Console.Write("y before call: "); y.show(); Console.WriteLine(); // exchange the objects to which x and y refer x.swap(ref x, ref y); Console.Write("x after call: "); x.show(); Console.Write("y after call: "); y.show(); } }