c# - Why can't i pass Dictionary<string, string> to IEnumerable<KeyValuePair<string, string>> as generic type -
i explanation. have generic class list of type t , execute delegate method on want pass ienumerable class able treat list, dictionary, etc.
assuming code :
public static class genericclass<t> { public delegate void processdelegate(ref ienumerable<t> p_entitieslist); public static void executeprocess(ref ienumerable<t> p_entitieslist, processdelegate p_delegate) { p_delegate(ref p_entitieslist); } } public static void main() { genericclass<keyvaluepair<string, string>.processdelegate delegateprocess = new genericclass<keyvaluepair<string, string>.processdelegate( delegate (ref ienumerable<keyvaluepair<string, string>> p_entitieslist) { //treatment... }); dictionary<string, string> dic = new dictionary<string, string>; genericclass<keyvaluepair<string, string>>.executeprocess(ref dic, delegateprocess); //i error : // cannot convert ref dictionary<string, string> ref ienumerable<keyvaluepair<string, string>> }
i explanation why can't pass dictionnary ienumerable of keyvaluepair because dictionary inherit ienumerable , use keyvaluepair.
also, better way doing ?
because it's ref
parameter.
a ref
parameter means method can assign new value field / variable passed caller.
had code been legal, method able assign list<keyvaluepair<string, string>>
, wrong.
you should not use ref
parameters.
Comments
Post a Comment