Demonstrate a generic interface : Generic Interface : Generics C# Source Code


Custom Search

C# Source Code » Generics » Generic Interface »

 

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() + "  ");

  }
}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Generics
» Generic Interface