# Demonstrate the ByTwos interface 2 : Interface « Class Interface « C# / C Sharp

Demonstrate the ByTwos interface 2

```/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
using System;

public interface ISeries {
int getNext(); // return next number in series
void reset(); // restart
void setStart(int x); // set starting value
}

// Use ISeries to generate a sequence of even numbers.
class ByTwos : ISeries {
int start;
int val;

public ByTwos() {
start = 0;
val = 0;
}

public int getNext() {
val += 2;
return val;
}

public void reset() {
val = start;
}

public void setStart(int x) {
start = x;
val = start;
}
}

// Use ISeries to implement a series of prime numbers.
class Primes : ISeries {
int start;
int val;

public Primes() {
start = 2;
val = 2;
}

public int getNext() {
int i, j;
bool isprime;

val++;
for(i = val; i < 1000000; i++) {
isprime = true;
for(j = 2; j < (i/j + 1); j++) {
if((i%j)==0) {
isprime = false;
break;
}
}
if(isprime) {
val = i;
break;
}
}
return val;
}

public void reset() {
val = start;
}

public void setStart(int x) {
start = x;
val = start;
}
}

public class SeriesDemo2 {
public static void Main() {
ByTwos twoOb = new ByTwos();
Primes primeOb = new Primes();
ISeries ob;

for(int i=0; i < 5; i++) {
ob = twoOb;
Console.WriteLine("Next ByTwos value is " +
ob.getNext());
ob = primeOb;
Console.WriteLine("Next prime number is " +
ob.getNext());
}
}
}

```

### Related examples in the same category

 1 Interface with method name conflicts 2 Demonstrate the ByTwos interface 3 Use a property in an interface 4 Add an indexer in an interface 5 One interface can inherit another 6 Explicitly implement an interface member 7 Use explicit implementation to remove ambiguity 8 Two class inherit one interface 9 Using interface 3 10 illustrates interfaces 11 illustrates implementing multiple interfaces 12 illustrates inheriting from a class and implementing multiple interfaces 13 illustrates casting an object to an interface 14 illustrates deriving an interface from one interface 15 illustrates deriving an interface from multiple interfaces 16 illustrates an explicit interface member implementation 17 illustrates interface member hiding 18 Demonstrates the use of a simple interface 19 Interface demo 20 Interface demo: implements two interfaces 21 Interface casting 22 Overriding Interfaces 23 Overriding Interfaces: Tester Overriding InterfacesAs 24 A safe method of determining whether a class implements a particular interface 25 Interfaces:Working with Interfaces 26 Reimplementation of Interfaces 27 Interface with event