c# - How to specify a list selection method? -
i've got method computes list. @ points in algorithm single element list needs chosen. doesn't matter element chosen, i'd leave user decide.
right now, i've added extension method ilist<t>.random() takes random element. .first() have worked equally well. supposing want let user pick method used, or perhaps entirely different method, how look?
i thinking using enum limited options, , wrap each of these calls in switch , call appropriate function. but maybe some sort of lambda function more appropriate?
this method needs used in 2 different places, once on list<char> , once on list<string>. want use same method both.
this isn't gui app. i'm trying decide how design api.
specifically, want have field like
public func<ilist<t>, t> selectelement = list => list.first(); which used in method,
public string reverse(string pattern, ilist<object> args = null, idictionary<string, object> kwargs = null) but generic fields aren't possible. i'm looking alternative solution. 1 make selectelement method argument reverse(), make generic... hoping keep @ class-level re-usability. don't want pass more args function if can it.
edit: full source code
how this:
public class myclass { public static class c<t> { public static func<ilist<t>, t> selectelement; } public int test(ilist<int> list) { return c<int>.selectelement(list); } } static class program { static void main(string[] args) { myclass.c<char>.selectelement = xs => xs.first(); myclass.c<int>.selectelement = xs => xs.first(); var list = new list<int>(new int[] { 1, 2, 3 }); var c = new myclass(); var v = c.test(list); console.writeline(v); } }
Comments
Post a Comment