c# - IList in interface, List in implementation -
in interfaces there typically ilist<isometype>
represent list-type members , expect implementation supporting add
method.
but in interface implementation, there
ilist<isometype> = new list<isometype>()
and everytime use list, have cast, example
(this.mylist list<imytype>).addrange(sometype.tolist());
is there better way this? how avoid cast?
- edit ask more information -
instead of extension method, there small linq expression solve that?
ilist<string> list = new list<string>(); var items = new[] { "1", "2", "3" }; items.tolist().foreach(x => list.add(x));
but not being straight forward, inverts being done. (the action on items add, not on list).
anything better that? possible can done on list?
addrange not method on ilist, add is, can make extension method:
public static void addrange<t>(this ilist<t> list, ienumerable<t> values) { foreach(var value in values) list.add(value); }
you in extension method check if list instance of list in case can call addrange directly
Comments
Post a Comment