c# - Breaking out of a recursive loop to check a condition, is this possible? -
i have situation need use same recursive loop check different conditions before looping around.
here recursive method;
private static categoryfeaturetype getparentcategorytype( dms.productlocation productlocation, dms.productlocation[] productlocations, dictionary<string, categoryfeaturetype> categoryfeaturetypes, int level) { categoryfeaturetype categoryfeaturetype = null; categoryfeaturetypes .trygetvalue(productlocation.externalchannelcode, out categoryfeaturetype); // here want return object perform conditional check // , come method if needed. return categoryfeaturetype; if (conditiontypecollection == null && productlocation.productlocationidforparent.hasvalue) { dms.productlocation parentlocation = productlocations .firstordefault(p => p.id == productlocation.productlocationidforparent); if (parentlocation != null) { conditiontypecollection = getparentcategorytypecollection( parentlocation, productlocations, categoryfeaturetypes, level + 1); } } }
normally, i'd have conditional check inside of method needs used in different situations use different conditional checks.
i use create method each conditional check i'd rather not this.
if possible in c# or going incorrect way?
you have callers pass in delegate returns bool
(say, predicate<categoryfeaturetype>
). getparentcategorytype
method invoke delegate , use returned true
or false
value dictate if break. when recursing, can pass same delegate through:
private static categoryfeaturetype getparentcategorytype( dms.productlocation productlocation, dms.productlocation[] productlocations, dictionary<string, categoryfeaturetype> categoryfeaturetypes, int level, predicate<categoryfeaturetype> shouldbreak) { categoryfeaturetype categoryfeaturetype = null; categoryfeaturetypes.trygetvalue(productlocation.externalchannelcode, out categoryfeaturetype); if (shouldbreak(categoryfeaturetype)) return categoryfeaturetype; if (conditiontypecollection == null && productlocation.productlocationidforparent.hasvalue) { dms.productlocation parentlocation = productlocations.firstordefault(p => p.id == productlocation.productlocationidforparent); if (parentlocation != null) { conditiontypecollection = getparentcategorytypecollection( parentlocation, productlocations, categoryfeaturetypes, level + 1, shouldbreak); } } }
then can wrap method ones pass in predefined delegate:
private static categoryfeaturetype getparentcategorytypewheretypeisawesome( dms.productlocation productlocation, dms.productlocation[] productlocations, dictionary<string, categoryfeaturetype> categoryfeaturetypes, int level) { predicate<categoryfeaturetype> returncheck = categoryfeaturetype => { return categoryfeaturetype.coolness == "awesome"; }; return getparentcategorytype( productlocation, productlocations, categoryfeaturetypes, level, returncheck); }
if wanted execute without conditional check, check null delegate or can pass in delegate returns false
:
if (shouldbreak != null && shouldbreak(categoryfeaturetype)) return categoryfeaturetype;
or empty wrapper:
private static categoryfeaturetype getparentcategorytypeneverexit( dms.productlocation productlocation, dms.productlocation[] productlocations, dictionary<string, categoryfeaturetype> categoryfeaturetypes, int level) { predicate<categoryfeaturetype> returncheck = categoryfeaturetype => { return false; }; return getparentcategorytype( productlocation, productlocations, categoryfeaturetypes, level, returncheck); }
Comments
Post a Comment