Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Generic Interface basic
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public interface ISeries<T> { T GetNext(); // return next element in series void Reset(); // restart the series void SetStart(T v); // set the starting element } // Implement ISeries. class ByTwos<T> : ISeries<T> { T start; T val; // This delegate defines the form of a method // that will be called when the next element in // the series is needed. public delegate T IncByTwo(T v); // This delegate reference will be assigned the // method passed to the ByTwos constructor. IncByTwo incr; public ByTwos(){} public ByTwos(IncByTwo incrMeth) { start = default(T); val = default(T); Console.WriteLine(val.GetType().Name.ToString()); incr = incrMeth; } public T GetNext() { //Console.WriteLine(val.GetType().Name.ToString()); val = incr(val); Console.WriteLine(val.GetType().Name.ToString()); return val; } public void Reset() { val = start; } public void SetStart(T v) { val=v; /*start = v; val = start;*/ } } class ByOne<T,V>:ISeries<T> { public T GetNext() { T t=default(T); return t; } // return next element in series public void Reset() { } // restart the series public void SetStart(T v) { }// set the starting element } class ThreeD { public int x, y, z; public ThreeD(int a, int b, int c) { x = a; y = b; z = c; } } public class Program { // Define plus two for int. static int IntPlusTwo(int v) { return v + 2; } // Define plus two for double. static double DoublePlusTwo(double v) { return v + 2.0; } // Define plus two for ThreeD. static ThreeD ThreeDPlusTwo(ThreeD v) { if(v==null) return new ThreeD(01, 01, 01); else return new ThreeD(v.x + 2, v.y + 2, v.z + 2); } public static void Main(string[] args) {// Demonstrate int series. ByTwos<int> intBT = new ByTwos<int>(IntPlusTwo); for(int i=0; i < 5; i++) Console.Write(intBT.GetNext() + " "); Console.WriteLine(); // Demonstrate double series. ByTwos<double> dblBT = new ByTwos<double>(DoublePlusTwo); dblBT.SetStart(11.4); for(int i=0; i < 5; i++) Console.Write(dblBT.GetNext() + " "); Console.WriteLine(); // Demonstrate ThreeD series. ByTwos<ThreeD> ThrDBT = new ByTwos<ThreeD>(ThreeDPlusTwo); ThreeD coord; for(int i=0; i < 5; i++) { coord = ThrDBT.GetNext(); Console.Write(coord.x + "," + coord.y + "," + coord.z + " "); } Console.WriteLine(); } } }
run
|
edit
|
history
|
help
0
Lab8
Find duplicates in a given integer array
BC150401319
23
Main 5.6
fuck this
Singleton Example
Reverse String
linked list reversal recursive
Main5