c# - Is it possible to create an interface this general? -


i'm going creating lot of classes like

public static class m2sa {     // median of 2 sorted arrays      public static int method ( int[] a, int[] b )     {         int m = a.length, n = b.length;         if((m | n) == 0)              throw new argumentexception("a , b cannot both empty");         int = 0, j = 0, k = (m + n)/2;          while((i + j) < k)         {             if(i == m) ++j;             else if(j == n || a[i] <= b[j]) ++i;             else ++j;         }         if(i == m) return b[j];         else if(j == n) return a[i];         else return math.min(a[i],b[j]);     }      public static int alternative ( int[] a, int[] b )     {         if ((a.length | b.length) == 0)             throw new argumentexception("a , b cannot both empty");         int[] mergedandsorted = a.concat(b).orderby(x => x).toarray();         return mergedandsorted[mergedandsorted.length / 2];     }      public static bool test ()     {         return false;//placeholder - haven't implemented yet     } } 

in implement

  • a public static method named method
  • a public static method named alternative has same signature method
  • a method public static bool test tests whether method , alternative produce equivalent output given set of generated input.

the classes may have other methods serve helpers.

is there way can create interface general enough requires above, doesn't know beyond that? or require methods have signatures?

for example, might have class

public static class unstablepartition {      public static void intswap(ref int a, ref int b)     {         // i'm amazed there isn't method in .net library (???)         int temp = a;         = b;         b = temp;     }      public delegate bool unarypredicate (int i);      public static void method ( int[] arr, unarypredicate pred )     {         for(int = 0, j = arr.length; < j; )         {             if (!pred(arr[i])) ++i;             else if (pred(arr[j])) --j;             else intswap(ref arr[i],ref arr[j]);         }     }      public static void alternative(int[] arr, unarypredicate pred)     {         int[] partioned = new int[arr.length];         (int ai = 0, pi = 0, pj = partioned.length; ai < arr.length; ++ai)         {             if (pred(arr[ai])) partioned[pj--] = arr[ai];             else partioned[pi++] = arr[ai];         }         array.copy(partioned, arr, partioned.length);     }       public static bool test()     {         return false;//placeholder - haven't implemented yet     }  } 

so want interface (i know below completely invalid ...)

public static interface interviewquestion {     public static method;     public static alternative;     public static bool test(); } 

and i'll implement like

public static class m2sa : interviewquestion 

interfaces (c# programming guide) can contain methods, properties, events, indexers, or combination of 4 member types... interface can't contain constants, fields, operators, instance constructors, destructors, or types. interface members automatically public, , can't include access modifiers. members can't static.

it means interface like

public interface interviewquestion {     int method(int[] a, int[] b);     int alternative(int[] a, int[] b);     bool test(); } 

and classes like

public class m2sa : interviewquestion {     public int alternative(int[] a, int[] b)     {         // implementation     }      public int method(int[] a, int[] b)     {         // implementation     }      public bool test()     {         // implementation     } }  public class unstablepartition : interviewquestion {     public int alternative(int[] a, int[] b)     {         // implementation     }      public int method(int[] a, int[] b)     {         // implementation     }      public bool test()     {         // implementation     } } 

or can use abstract class. implementation like

public abstract class interviewquestion {     public abstract int method(int[] a, int[] b);     public abstract int alternative(int[] a, int[] b);     public abstract bool test(); }  public class m2sa : interviewquestion {     public override int alternative(int[] a, int[] b)     {         // implementation     }      public override int method(int[] a, int[] b)     {         // implementation     }      public override bool test()     {         // implementation     } }  public class unstablepartition : interviewquestion {     public override int alternative(int[] a, int[] b)     {         // implementation     }      public override int method(int[] a, int[] b)     {         // implementation     }      public override bool test()     {         // implementation     } } 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo