using System;
public class Foo
{
public int i;
}
class RefTest2App
{
public static void ChangeValue(Foo f)
{
f.i = 42;
}
static void Main(string[] args)
{
Foo test = new Foo();
test.i = 6;
Console.WriteLine("BEFORE METHOD CALL");
Console.WriteLine("test.i={0}", test.i);
Console.WriteLine();
ChangeValue(test);
Console.WriteLine("AFTER METHOD CALL");
Console.WriteLine("test.i={0}", test.i);
}
}
Author: coder
C# Classes Member Functions
using System;
public class MemberFunctions
{
public static void Main()
{
Point myPoint = new Point(10, 15);
Console.WriteLine("myPoint.X {0}", myPoint.GetX());
Console.WriteLine("myPoint.Y {0}", myPoint.GetY());
}
}
class Point
{
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
// accessor functions
public int GetX() {return(x);}
public int GetY() {return(y);}
// variables now private
int x;
int y;
}
Overloading Classes
/*
* 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 OverloadingClasses
{
static void Main(string[] args)
{
A MyA = new A();
MyA.Display();
MyA.Display(10);
}
}
class A
{
public void Display()
{
Console.WriteLine("No Params Display Method");
}
public void Display(int A)
{
Console.WriteLine("Overloaded Display {0}", A);
}
}
}
A simple example of recursion
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A simple example of recursion.
using System;
class Factorial {
// This is a recursive function.
public int factR(int n) {
int result;
if(n==1) return 1;
result = factR(n-1) * n;
return result;
}
// This is an iterative equivalent.
public int factI(int n) {
int t, result;
result = 1;
for(t=1; t <= n; t++) result *= t;
return result;
}
}
public class Recursion {
public static void Main() {
Factorial f = new Factorial();
Console.WriteLine("Factorials using recursive method.");
Console.WriteLine("Factorial of 3 is " + f.factR(3));
Console.WriteLine("Factorial of 4 is " + f.factR(4));
Console.WriteLine("Factorial of 5 is " + f.factR(5));
Console.WriteLine();
Console.WriteLine("Factorials using iterative method.");
Console.WriteLine("Factorial of 3 is " + f.factI(3));
Console.WriteLine("Factorial of 4 is " + f.factI(4));
Console.WriteLine("Factorial of 5 is " + f.factI(5));
}
}
[/csharp]
Automatic type conversions can affect overloaded method resolution
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/* Automatic type conversions can affect
overloaded method resolution. */
using System;
class Overload2 {
public void f(int x) {
Console.WriteLine("Inside f(int): " + x);
}
public void f(double x) {
Console.WriteLine("Inside f(double): " + x);
}
}
public class TypeConv {
public static void Main() {
Overload2 ob = new Overload2();
int i = 10;
double d = 10.1;
byte b = 99;
short s = 10;
float f = 11.5F;
ob.f(i); // calls ob.f(int)
ob.f(d); // calls ob.f(double)
ob.f(b); // calls ob.f(int) -- type conversion
ob.f(s); // calls ob.f(int) -- type conversion
ob.f(f); // calls ob.f(double) -- type conversion
}
}
Demonstrate method overloading
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate method overloading.
using System;
class Overload {
public void ovlDemo() {
Console.WriteLine("No parameters");
}
// Overload ovlDemo for one integer parameter.
public void ovlDemo(int a) {
Console.WriteLine("One parameter: " + a);
}
// Overload ovlDemo for two integer parameters.
public int ovlDemo(int a, int b) {
Console.WriteLine("Two parameters: " + a + " " + b);
return a + b;
}
// Overload ovlDemo for two double parameters.
public double ovlDemo(double a, double b) {
Console.WriteLine("Two double parameters: " +
a + " "+ b);
return a + b;
}
}
public class OverloadDemo {
public static void Main() {
Overload ob = new Overload();
int resI;
double resD;
// call all versions of ovlDemo()
ob.ovlDemo();
Console.WriteLine();
ob.ovlDemo(2);
Console.WriteLine();
resI = ob.ovlDemo(4, 6);
Console.WriteLine("Result of ob.ovlDemo(4, 6): " +
resI);
Console.WriteLine();
resD = ob.ovlDemo(1.1, 2.32);
Console.WriteLine("Result of ob.ovlDemo(1.1, 2.2): " +
resD);
}
}
Return an array
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Return an array.
using System;
class Factor {
/* Return an array containing the factors of num.
On return, numfactors will contain the number of
factors found. */
public int[] findfactors(int num, out int numfactors) {
int[] facts = new int[80]; // size of 80 is arbitrary
int i, j;
// find factors and put them in the facts array
for(i=2, j=0; i < num/2 + 1; i++)
if( (num%i)==0 ) {
facts[j] = i;
j++;
}
numfactors = j;
return facts;
}
}
public class FindFactors {
public static void Main() {
Factor f = new Factor();
int numfactors;
int[] factors;
factors = f.findfactors(1000, out numfactors);
Console.WriteLine("Factors for 1000 are: ");
for(int i=0; i < numfactors; i++)
Console.Write(factors[i] + " ");
Console.WriteLine();
}
}
[/csharp]