Using Interface References


You might be somewhat surprised to learn that you can declare a reference variable of an interface type. In other words, you can create an interface reference variable. Such a variable can refer to any object that implements its interface. When you call a method on an object through an interface reference, it is the version of the method implemented by the object that is executed. This process is similar to using a base class reference to access a derived class object, as described in Chapter 11.

The following example illustrates the use of an interface reference. It uses the same interface reference variable to call methods on objects of both ByTwos and Primes.

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

The output from the program is shown here:

 Next ByTwos value is 2 Next prime number is 3 Next ByTwos value is 4 Next prime number is 5 Next ByTwos value is 6 Next prime number is 7 Next ByTwos value is 8 Next prime number is 11 Next ByTwos value is 10 Next prime number is 13

In Main( ), ob is declared to be a reference to an ISeries interface. This means that it can be used to store references to any object that implements ISeries. In this case, it is used to refer to twoOb and primeOb, which are objects of type ByTwos and Primes, respectively, which both implement ISeries.

One other point: An interface reference variable has knowledge only of the methods declared by its interface declaration. Thus, an interface reference cannot be used to access any other variables or methods that might be supported by the object.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net