C# Programming Purvis Samsoodeen
listing 1
public interface ISeries {
int GetNext(); // return next number in series
void Reset(); // restart
void SetStart(int x); // set starting value
}
listing 2
// Implement ISeries.
class ByTwos : ISeries {
int start;
int val;
public ByTwos() {
start = 0;
val = 0;
}
public int GetNext() {
val += 2;
return val;
}
public void Reset() {
start = 0;
val = 0;
}
public void SetStart(int x) {
start = x;
val = x;
}
}
listing 3
// Demonstrate the ByTwos interface.
using System;
class ISeriesDemo {
static void Main() {
ByTwos ob = new ByTwos();
for(int i=0; i < 5; i++)
Console.WriteLine("Next value is " +
ob.GetNext());
Console.WriteLine("\nResetting");
ob.Reset();
for(int i=0; i < 5; i++)
Console.WriteLine("Next value is " +
ob.GetNext());
Console.WriteLine("\nStarting at 100");
ob.SetStart(100);
for(int i=0; i < 5; i++)
Page 1 of 14
C# Programming Purvis Samsoodeen
Console.WriteLine("Next value is " +
ob.GetNext());
}
}
listing 4
// Implement ISeries and add GetPrevious().
class ByTwos : ISeries {
int start;
int val;
int prev;
public ByTwos() {
start = 0;
val = 0;
prev = -2;
}
public int GetNext() {
prev = val;
val += 2;
return val;
}
public void Reset() {
start = 0;
val = 0;
prev = -2;
}
public void SetStart(int x) {
start = x;
val = x;
prev = x - 2;
}
// A method not specified by ISeries.
int GetPrevious() {
return prev;
}
}
listing 5
// Implement ISeries.
class ByThrees : ISeries {
int start;
int val;
public ByThrees() {
start = 0;
val = 0;
}
public int GetNext() {
val += 3;
Page 2 of 14
C# Programming Purvis Samsoodeen
return val;
}
public void Reset() {
start = 0;
val = 0;
}
public void SetStart(int x) {
start = x;
val = x;
}
}
listing 6
// Demonstrate interface references.
using System;
// Define the interface
public interface ISeries {
int GetNext(); // return next number in series
void Reset(); // restart
void SetStart(int x); // set starting value
}
// Implement ISeries one way.
class ByTwos : ISeries {
int start;
int val;
public ByTwos() {
start = 0;
val = 0;
}
public int GetNext() {
val += 2;
return val;
}
public void Reset() {
start = 0;
val = 0;
}
public void SetStart(int x) {
start = x;
val = x;
}
}
// Implement ISeries another way.
class ByThrees : ISeries {
int start;
int val;
Page 3 of 14
C# Programming Purvis Samsoodeen
public ByThrees() {
start = 0;
val = 0;
}
public int GetNext() {
val += 3;
return val;
}
public void Reset() {
start = 0;
val = 0;
}
public void SetStart(int x) {
start = x;
val = x;
}
}
class ISeriesDemo2 {
static void Main() {
ByTwos twoOb = new ByTwos();
ByThrees threeOb = new ByThrees();
ISeries ob;
for(int i=0; i < 5; i++) {
ob = twoOb;
Console.WriteLine("Next ByTwos value is " +
ob.GetNext());
ob = threeOb;
Console.WriteLine("Next ByThrees value is " +
ob.GetNext());
}
}
}
listing 7
// A character queue interface.
public interface ICharQ {
// Put a character into the queue.
void Put(char ch);
// Get a character from the queue.
char Get();
}
listing 8
// Demonstrate the ICharQ interface.
using System;
// A simple, fixed-size queue class for characters.
Page 4 of 14
C# Programming Purvis Samsoodeen
class SimpleQueue : ICharQ {
char[] q; // this array holds the queue
int putloc, getloc; // the put and get indices
// Construct an empty queue given its size.
public SimpleQueue(int size) {
q = new char[size+1]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a character into the queue.
public void Put(char ch) {
if(putloc==q.Length-1) {
Console.WriteLine(" -- Queue is full.");
return;
}
putloc++;
q[putloc] = ch;
}
// Get a character from the queue.
public char Get() {
if(getloc == putloc) {
Console.WriteLine(" -- Queue is empty.");
return (char) 0;
}
getloc++;
return q[getloc];
}
}
listing 9
// A circular queue.
class CircularQueue : ICharQ {
char[] q; // this array holds the queue
int putloc, getloc; // the put and get indices
// Construct an empty queue given its size.
public CircularQueue(int size) {
q = new char[size+1]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a character into the queue.
public void Put(char ch) {
/* Queue is full if either putloc is one less than
getloc, or if putloc is at the end of the array
and getloc is at the beginning. */
if(putloc+1==getloc ||
((putloc==q.Length-1) && (getloc==0))) {
Console.WriteLine(" -- Queue is full.");
return;
}
Page 5 of 14
C# Programming Purvis Samsoodeen
putloc++;
if(putloc==q.Length) putloc = 0; // loop back
q[putloc] = ch;
}
// Get a character from the queue.
public char Get() {
if(getloc == putloc) {
Console.WriteLine(" -- Queue is empty.");
return (char) 0;
}
getloc++;
if(getloc==q.Length) getloc = 0; // loop back
return q[getloc];
}
}
listing 10
// A dynamic circular queue.
// This implementation automatically doubles the
// size of the queue when it is full.
class DynQueue : ICharQ {
char[] q; // this array holds the queue
int putloc, getloc; // the put and get indices
// Construct an empty queue given its size.
public DynQueue(int size) {
q = new char[size+1]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a character into the queue.
public void Put(char ch) {
/* If the queue is full, double the size of the
underlying array. */
if(putloc+1==getloc ||
((putloc==q.Length-1) && (getloc==0))) {
// Allocate a larger array for the queue.
char[] t = new char[q.Length * 2];
// Copy elements into the new array.
int i;
for(i=1; putloc != getloc; i++)
t[i] = Get();
// Reset the getloc and putloc indexes.
getloc = 0;
putloc = i-1;
// Make q refer to the new queue.
q = t;
}
Page 6 of 14
C# Programming Purvis Samsoodeen
putloc++;
if(putloc==q.Length) putloc = 0; // loop back
q[putloc] = ch;
}
// Get a character from the queue.
public char Get() {
if(getloc == putloc) {
Console.WriteLine(" -- Queue is empty.");
return (char) 0;
}
getloc++;
if(getloc==q.Length) getloc = 0; // loop back
return q[getloc];
}
}
listing 11
// Demonstrate the queues.
class IQDemo {
static void Main() {
SimpleQueue q1 = new SimpleQueue(10);
DynQueue q2 = new DynQueue(5);
CircularQueue q3 = new CircularQueue(10);
ICharQ iQ;
char ch;
int i;
// Assign iQ a reference to a simple, fixed-size queue.
iQ = q1;
// Put some characters into queue.
for(i=0; i < 10; i++)
iQ.Put((char) ('A' + i));
// Show the queue.
Console.Write("Contents of fixed-size queue: ");
for(i=0; i < 10; i++) {
ch = iQ.Get();
Console.Write(ch);
}
Console.WriteLine();
// Assign iQ a reference to a dynamic queue.
iQ = q2;
// Put some characters into dynamic queue.
for(i=0; i < 10; i++)
iQ.Put((char) ('Z' - i));
Page 7 of 14
C# Programming Purvis Samsoodeen
// Show the queue.
Console.Write("Contents of dynamic queue: ");
for(i=0; i < 10; i++) {
ch = iQ.Get();
Console.Write(ch);
}
Console.WriteLine();
// Assign iQ a reference to a circular queue.
iQ = q3;
// Put some characters into circular queue.
for(i=0; i < 10; i++)
iQ.Put((char) ('A' + i));
// Show the queue.
Console.Write("Contents of circular queue: ");
for(i=0; i < 10; i++) {
ch = iQ.Get();
Console.Write(ch);
}
Console.WriteLine();
// Put more characters into circular queue.
for(i=10; i < 20; i++)
iQ.Put((char) ('A' + i));
// Show the queue.
Console.Write("Contents of circular queue: ");
for(i=0; i < 10; i++) {
ch = iQ.Get();
Console.Write(ch);
}
Console.WriteLine("\nStore and consume from" +
" circular queue.");
// Use and consume from circular queue.
for(i=0; i < 20; i++) {
iQ.Put((char) ('A' + i));
ch = iQ.Get();
Console.Write(ch);
}
}
}
listing 12
// Use a property in an interface.
using System;
public interface ISeries {
// An interface property.
Page 8 of 14
C# Programming Purvis Samsoodeen
int Next {
get; // return the next number in series
set; // set next number
}
}
// Implement ISeries.
class ByTwos : ISeries {
int val;
public ByTwos() {
val = 0;
}
// Get or set value.
public int Next {
get {
val += 2;
return val;
}
set {
val = value;
}
}
}
// Demonstrate an interface property.
class ISeriesDemo3 {
static void Main() {
ByTwos ob = new ByTwos();
// Access series through a property.
for(int i=0; i < 5; i++)
Console.WriteLine("Next value is " + ob.Next);
Console.WriteLine("\nStarting at 21");
ob.Next = 21;
for(int i=0; i < 5; i++)
Console.WriteLine("Next value is " + ob.Next);
}
}
listing 13
// Add an indexer in an interface.
using System;
public interface ISeries {
// An interface property.
int Next {
get; // return the next number in series
set; // set next number
}
// An interface indexer.
int this[int index] {
Page 9 of 14
C# Programming Purvis Samsoodeen
get; // return the specified number in series
}
}
// Implement ISeries.
class ByTwos : ISeries {
int val;
public ByTwos() {
val = 0;
}
// Get or set a value using a property.
public int Next {
get {
val += 2;
return val;
}
set {
val = value;
}
}
// Get a value using an indexer.
public int this[int index] {
get {
val = 0;
for(int i=0; i<index; i++)
val += 2;
return val;
}
}
}
// Demonstrate an interface indexer.
class ISeriesDemo4 {
static void Main() {
ByTwos ob = new ByTwos();
// Access series through a property.
for(int i=0; i < 5; i++)
Console.WriteLine("Next value is " + ob.Next);
Console.WriteLine("\nStarting at 21");
ob.Next = 21;
for(int i=0; i < 5; i++)
Console.WriteLine("Next value is " +
ob.Next);
Console.WriteLine("\nResetting to 0");
ob.Next = 0;
// Access series through an indexer.
for(int i=0; i < 5; i++)
Console.WriteLine("Next value is " + ob[i]);
Page 10 of 14
C# Programming Purvis Samsoodeen
}
}
listing 14
// One interface can inherit another.
using System;
public interface IA {
void Meth1();
void Meth2();
}
// IB now includes Meth1() and Meth2() -- it adds Meth3().
public interface IB : IA {
void Meth3();
}
// This class must implement all of IA and IB.
class MyClass : IB {
public void Meth1() {
Console.WriteLine("Implement Meth1().");
}
public void Meth2() {
Console.WriteLine("Implement Meth2().");
}
public void Meth3() {
Console.WriteLine("Implement Meth3().");
}
}
class IFExtend {
static void Main() {
MyClass ob = new MyClass();
ob.Meth1();
ob.Meth2();
ob.Meth3();
}
}
listing 15
interface IMyIF {
int MyMeth(int x);
}
listing 16
class MyClass : IMyIF {
int IMyIF.MyMeth(int x) {
return x / 3;
}
}
listing 17
Page 11 of 14
C# Programming Purvis Samsoodeen
// Explicitly implement an interface member.
using System;
interface IEven {
bool IsOdd(int x);
bool IsEven(int x);
}
class MyClass : IEven {
// Explicit implementation.
bool IEven.IsOdd(int x) {
if((x%2) != 0) return true;
else return false;
}
// Normal implementation.
public bool IsEven(int x) {
IEven o = this; // reference to invoking object
return !o.IsOdd(x);
}
}
class Demo {
static void Main() {
MyClass ob = new MyClass();
bool result;
result = ob.IsEven(4);
if(result) Console.WriteLine("4 is even.");
else Console.WriteLine("3 is odd.");
// result = ob.IsOdd(); // Error, not exposed.
}
}
listing 18
// Use explicit implementation to remove ambiguity.
using System;
interface IMyIF_A {
int Meth(int x);
}
interface IMyIF_B {
int Meth(int x);
}
// MyClass implements both interfaces.
class MyClass : IMyIF_A, IMyIF_B {
IMyIF_A a_ob;
IMyIF_B b_ob;
// Explicitly implement the two Meth()s.
int IMyIF_A.Meth(int x) {
Page 12 of 14
C# Programming Purvis Samsoodeen
return x + x;
}
int IMyIF_B.Meth(int x) {
return x * x;
}
// Call Meth() through an interface reference.
public int MethA(int x){
a_ob = this;
return a_ob.Meth(x); // calls IMyIF_A
}
public int MethB(int x){
b_ob = this;
return b_ob.Meth(x); // calls IMyIF_B
}
}
class FQIFNames {
static void Main() {
MyClass ob = new MyClass();
Console.Write("Calling IMyIF_A.Meth(): ");
Console.WriteLine(ob.MethA(3));
Console.Write("Calling IMyIF_B.Meth(): ");
Console.WriteLine(ob.MethB(3));
}
}
listing 19
// Demonstrate a structure.
using System;
// Define a structure.
struct Account {
public string name;
public double balance;
public Account(string n, double b) {
name = n;
balance = b;
}
}
// Demonstrate Account structure.
class StructDemo {
static void Main() {
Account acc1 = new Account("Tom", 1232.22); // explicit constructor
Account acc2 = new Account(); // default constructor
Account acc3; // no constructor
Console.WriteLine(acc1.name + " has a balance of " + acc1.balance);
Console.WriteLine();
Page 13 of 14
C# Programming Purvis Samsoodeen
if(acc2.name == null) Console.WriteLine("acc2.name is null.");
Console.WriteLine("acc2.balance is " + acc2.balance);
Console.WriteLine();
// Must initialize acc3 prior to use.
acc3.name = "Mary";
acc3.balance = 99.33;
Console.WriteLine(acc3.name + " has a balance of " + acc3.balance);
}
}
listing 20
// Demonstrate an enumeration.
using System;
class EnumDemo {
enum Coin { Penny, Nickel, Dime, Quarter, HalfDollar, Dollar };
static void Main() {
Coin c; // declare an enum variable
// Use c to cycle through the enum by use of a for loop.
for(c = Coin.Penny; c <= Coin.Dollar; c++) {
Console.WriteLine(c + " has value of " + (int) c);
// Use an enumeration value to control a switch.
switch(c) {
case Coin.Nickel:
Console.WriteLine("A nickel is 5 pennies.");
break;
case Coin.Dime:
Console.WriteLine("A dime is 2 nickels.");
break;
case Coin.Quarter:
Console.WriteLine("A quarter is 5 nickels.");
break;
case Coin.HalfDollar:
Console.WriteLine("A half-dollar is 5 dimes.");
break;
case Coin.Dollar:
Console.WriteLine("A dollar is 10 dimes.");
break;
}
Console.WriteLine();
}
}
}
Page 14 of 14