Demonstrate a generic interface
using System;
public interface ISequence<T> {
T getNext();
void reset();
void setStart(T v);
}
class ByTwos<T> : ISequence<T> {
T start;
T val;
public delegate T IncByTwo(T v);
IncByTwo incr;
public ByTwos(IncByTwo incrMeth) {
start = default(T);
val = default(T);
incr = incrMeth;
}
public T getNext() {
val = incr(val);
return val;
}
public void reset() {
val = start;
}
public void setStart(T v) {
start = v;
val = start;
}
}
class GenIntfDemo {
static int intPlusTwo(int v) {
return v + 2;
}
static double doublePlusTwo(double v) {
return v + 2.0;
}
public static void Main() {
ByTwos<int> intBT = new ByTwos<int>(intPlusTwo);
for(int i=0; i < 5; i++)
Console.Write(intBT.getNext() + " ");
Console.WriteLine();
ByTwos<double> dblBT = new ByTwos<double>(doublePlusTwo);
dblBT.setStart(11.4);
for(int i=0; i < 5; i++)
Console.Write(dblBT.getNext() + " ");
}
}
Related examples in the same category