Break from recursive loop to check state, is this possible?

I have a situation where I need to use the same recursive loop, but check different conditions before looping around.

Here is my 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 is where i want to return the object to perform a conditional check
    // and come back into the 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);
        }
    }
}

Usually I should have a conditional check inside the method, but since it should be used in different situations that will use different conditional checks.

I could use the create method for each conditional check, but I would prefer not to.

If possible in C # or am I mistaken?

+4
source share
2 answers

, bool (, Predicate<CategoryFeatureType>). GetParentCategoryType true false, , . :

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);
       }
   }
}

, :

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);
}

, , , false:

if (shouldBreak != null && shouldBreak(categoryFeatureType))   
    return categoryFeatureType;

:

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);
}
+2

, , , Predicate .

+1
source

Source: https://habr.com/ru/post/1536156/


All Articles