Two class inherit one interface

/*
C#: The Complete Reference
by Herbert Schildt

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

// An encryption interface.
public interface ICipher {
string encode(string str);
string decode(string str);
}

/* A simple implementation of ICipher that codes
a message by shifting each character 1 position
higher. Thus, A becomes B, and so on. */
class SimpleCipher : ICipher {

// Return an encoded string given plaintext.
public string encode(string str) {
string ciphertext = “”;

for(int i=0; i < str.Length; i++) ciphertext = ciphertext + (char) (str[i] + 1); return ciphertext; } // Return an decoded string given ciphertext. public string decode(string str) { string plaintext = ""; for(int i=0; i < str.Length; i++) plaintext = plaintext + (char) (str[i] - 1); return plaintext; } } /* This implementation of ICipher uses bit manipulations and key. */ class BitCipher : ICipher { ushort key; // Specify a key when constructing BitCiphers. public BitCipher(ushort k) { key = k; } // Return an encoded string given plaintext. public string encode(string str) { string ciphertext = ""; for(int i=0; i < str.Length; i++) ciphertext = ciphertext + (char) (str[i] ^ key); return ciphertext; } // Return an decoded string given ciphertext. public string decode(string str) { string plaintext = ""; for(int i=0; i < str.Length; i++) plaintext = plaintext + (char) (str[i] ^ key); return plaintext; } } // Demonstrate ICipher. public class ICipherDemo { public static void Main() { ICipher ciphRef; BitCipher bit = new BitCipher(27); SimpleCipher sc = new SimpleCipher(); string plain; string coded; // first, ciphRef refers to the simple cipher ciphRef = sc; Console.WriteLine("Using simple cipher."); plain = "testing"; coded = ciphRef.encode(plain); Console.WriteLine("Cipher text: " + coded); plain = ciphRef.decode(coded); Console.WriteLine("Plain text: " + plain); // now, let ciphRef refer to the bitwise cipher ciphRef = bit; Console.WriteLine(" Using bitwise cipher."); plain = "testing"; coded = ciphRef.encode(plain); Console.WriteLine("Cipher text: " + coded); plain = ciphRef.decode(coded); Console.WriteLine("Plain text: " + plain); } } [/csharp]

Use explicit implementation to remove ambiguity


   

/*
C#: The Complete Reference 
by Herbert Schildt 

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


// 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 { 
 
  // explicitly implement the two meth()s 
  int IMyIF_A.meth(int x) { 
    return x + x; 
  } 
  int IMyIF_B.meth(int x) { 
    return x * x; 
  } 
 
  // call meth() through an interface reference. 
  public int methA(int x){ 
    IMyIF_A a_ob; 
    a_ob = this; 
    return a_ob.meth(x); // calls IMyIF_A 
  } 
 
  public int methB(int x){ 
    IMyIF_B b_ob; 
    b_ob = this; 
    return b_ob.meth(x); // calls IMyIF_B 
  } 
} 
 
public class FQIFNames { 
  public 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)); 
  } 
}

           
          


Explicitly implement an interface member


   

/*
C#: The Complete Reference 
by Herbert Schildt 

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


// 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); 
  } 
} 
 
public class Demo { 
  public 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 
  } 
}

           
          


One interface can inherit another


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// One interface can inherit another. 
 
using System; 
 
public interface A { 
  void meth1(); 
  void meth2(); 
} 
 
// B now includes meth1() and meth2() -- it adds meth3(). 
public interface B : A { 
  void meth3(); 
} 
 
// This class must implement all of A and B 
class MyClass : B { 
  public void meth1() { 
    Console.WriteLine("Implement meth1()."); 
  } 
 
  public void meth2() { 
    Console.WriteLine("Implement meth2()."); 
  } 
 
  public void meth3() { 
    Console.WriteLine("Implement meth3()."); 
  } 
} 
 
public class IFExtend { 
  public static void Main() { 
    MyClass ob = new MyClass(); 
 
    ob.meth1(); 
    ob.meth2(); 
    ob.meth3(); 
  } 
}

           
          


Add an indexer in an interface

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// 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] {
get; // return the specified number in series
}
}

// Implement ISeries.
class ByTwos : ISeries {
int val;

public ByTwos() {
val = 0;
}

// get or set value using a property
public int next {
get {
val += 2;
return val;
}
set {
val = value;
}
}

// get a value using an index
public int this[int index] {
get {
val = 0;
for(int i=0; i

Use a property in an interface

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use a property in an interface.

using System;

public interface ISeries {
// an interface property
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.
public class SeriesDemo3 {
public 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(" Starting at 21"); ob.next = 21; for(int i=0; i < 5; i++) Console.WriteLine("Next value is " + ob.next); } } [/csharp]

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()); } } } [/csharp]