/*
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);
}
}
Language Basics
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();
}
}
Use two out parameters
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use two out parameters.
using System;
class Num {
/* Determine if x and v have a common denominator.
If so, return least and greatest common denominators in
the out parameters. */
public bool isComDenom(int x, int y,
out int least, out int greatest) {
int i;
int max = x < y ? x : y;
bool first = true;
least = 1;
greatest = 1;
// find least and treatest common denominators
for(i=2; i <= max/2 + 1; i++) {
if( ((y%i)==0) & ((x%i)==0) ) {
if(first) {
least = i;
first = false;
}
greatest = i;
}
}
if(least != 1) return true;
else return false;
}
}
public class DemoOut {
public static void Main() {
Num ob = new Num();
int lcd, gcd;
if(ob.isComDenom(231, 105, out lcd, out gcd)) {
Console.WriteLine("Lcd of 231 and 105 is " + lcd);
Console.WriteLine("Gcd of 231 and 105 is " + gcd);
}
else
Console.WriteLine("No common denominator for 35 and 49.");
if(ob.isComDenom(35, 51, out lcd, out gcd)) {
Console.WriteLine("Lcd of 35 and 51 " + lcd);
Console.WriteLine("Gcd of 35 and 51 is " + gcd);
}
else
Console.WriteLine("No common denominator for 35 and 51.");
}
}
[/csharp]