/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use out.
using System;
class Decompose {
/* Decompose a floating-point value into its
integer and fractional parts. */
public int parts(double n, out double frac) {
int whole;
whole = (int) n;
frac = n - whole; // pass fractional part back through frac
return whole; // return integer portion
}
}
public class UseOut {
public static void Main() {
Decompose ob = new Decompose();
int i;
double f;
i = ob.parts(10.125, out f);
Console.WriteLine("Integer portion is " + i);
Console.WriteLine("Fractional part is " + f);
}
}
Language Basics
Swap two values
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Swap two values.
using System;
class Swap {
// This method now changes its arguments.
public void swap(ref int a, ref int b) {
int t;
t = a;
a = b;
b = t;
}
}
public class SwapDemo {
public static void Main() {
Swap ob = new Swap();
int x = 10, y = 20;
Console.WriteLine("x and y before call: " + x + " " + y);
ob.swap(ref x, ref y);
Console.WriteLine("x and y after call: " + x + " " + y);
}
}
Use ref to pass a value type by reference
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use ref to pass a value type by reference.
using System;
class RefTest {
/* This method changes its arguments.
Notice the use of ref. */
public void sqr(ref int i) {
i = i * i;
}
}
public class RefDemo {
public static void Main() {
RefTest ob = new RefTest();
int a = 10;
Console.WriteLine("a before call: " + a);
ob.sqr(ref a); // notice the use of ref
Console.WriteLine("a after call: " + a);
}
}
Objects are passed by reference
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Objects are passed by reference.
using System;
class Test {
public int a, b;
public Test(int i, int j) {
a = i;
b = j;
}
/* Pass an object. Now, ob.a and ob.b in object
used in the call will be changed. */
public void change(Test ob) {
ob.a = ob.a + ob.b;
ob.b = -ob.b;
}
}
public class CallByRef {
public static void Main() {
Test ob = new Test(15, 20);
Console.WriteLine("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.change(ob);
Console.WriteLine("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
}
Simple types are passed by value
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Simple types are passed by value.
using System;
class Test {
/* This method causes no change to the arguments
used in the call. */
public void noChange(int i, int j) {
i = i + j;
j = -j;
}
}
public class CallByValue {
public static void Main() {
Test ob = new Test();
int a = 15, b = 20;
Console.WriteLine("a and b before call: " +
a + " " + b);
ob.noChange(a, b);
Console.WriteLine("a and b after call: " +
a + " " + b);
}
}
Objects can be passed to methods
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Objects can be passed to methods.
using System;
class MyClass {
int alpha, beta;
public MyClass(int i, int j) {
alpha = i;
beta = j;
}
/* Return true if ob contains the same values
as the invoking object. */
public bool sameAs(MyClass ob) {
if((ob.alpha == alpha) & (ob.beta == beta))
return true;
else return false;
}
// Make a copy of ob.
public void copy(MyClass ob) {
alpha = ob.alpha;
beta = ob.beta;
}
public void show() {
Console.WriteLine("alpha: {0}, beta: {1}",
alpha, beta);
}
}
public class PassOb {
public static void Main() {
MyClass ob1 = new MyClass(4, 5);
MyClass ob2 = new MyClass(6, 7);
Console.Write("ob1: ");
ob1.show();
Console.Write("ob2: ");
ob2.show();
if(ob1.sameAs(ob2))
Console.WriteLine("ob1 and ob2 have the same values.");
else
Console.WriteLine("ob1 and ob2 have different values.");
Console.WriteLine();
// now, make ob1 a copy of ob2
ob1.copy(ob2);
Console.Write("ob1 after copy: ");
ob1.show();
if(ob1.sameAs(ob2))
Console.WriteLine("ob1 and ob2 have the same values.");
else
Console.WriteLine("ob1 and ob2 have different values.");
}
}
Passing Parameters By Value and By Ref
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_5___Building_Your_Own_Classes
{
public class PassingParametersByValueandByRef
{
static void Main(string[] args)
{
int MyInt = 5;
MyIntArray = new int[10];
ObjectCount++;
Method2();
Method2(1, 2);
MyMethodRef(ref MyInt);
Method2(new int[] { 1, 2, 3, 4 });
}
static private int MyInt = 5;
static public int MyInt2 = 10;
static public int[] MyIntArray;
private static int ObjectCount = 0;
static public int MyMethodRef(ref int myInt)
{
MyInt = MyInt + myInt;
return MyInt;
}
static public int MyMethod(int myInt)
{
MyInt = MyInt + myInt;
return MyInt;
}
static private long MyLongMethod(ref int myInt)
{
return myInt;
}
static public void Method2(params int[] args)
{
for (int I = 0; I < args.Length; I++)
Console.WriteLine(args[I]);
}
}
}
[/csharp]