1

I have a list of integers (Levels). I want to initialize a nested Object of Filter called myFilter as below(Filter is a class with two properties: Value and NextFilter):

var myFilter  = new Fliter{
     Value = Levels[0],
     NextFilter = new Filter{
         Value = Levels[1],
         NextFilter = new Filter{
             Value = Levels[2],
             NextFilter = new Filter{
                 Value = Levels[n],
                 NextFilter = null
             }
        }
    }

}

Level's count is not static and depends on the input list (I have a multi select list that generates Level) How can I do that?

Elnaz
  • 2,854
  • 3
  • 29
  • 41

2 Answers2

0

Just make a constructor of Filter, that will get Levels array as a parameter, that will set it's Value as level[0], and init NextFilter = new Filter(level.Skip(1)). Something like that. And it will recursively initialize your object.

Artem Makarov
  • 874
  • 3
  • 14
  • 25
0

This is a classic event for using recursion - the technique of a method that calls itself:

public static Filter CreateFilter(List<int> values) => values.Any() ? new Filter //If the list contains elements, create the filter
{
    Value = values.First(), //assign the first item of the values to the value property
    NextFilter = CreateFilter(values.Skip(1).ToList()) //Create the rest of the nested object with the rest of the values
} : null; //If there aren't any items left in the list, return null and stop the recursion

You could of course do it in the constructor as well:

public Filter(List<int> values)
{
    if (!values.Any()) return;
    Value = values.First();
    NextFilter = values.Count > 1 ? new Filter(values.Skip(1).ToList()) : null;
}

For more information about recursion, take a look at this: https://www.dotnetperls.com/recursion, for more information on nested classes read through this: https://www.dotnetperls.com/nested-class.


A few more information on recursion:

You can actually achieve everything through recursion - you don't even need loops. That's the reason why in languages like Haskell loops don't exist. The simplest recursive function is:

public static void EndlessLoop()
{
    //Loop body
    EndlessLoop();
}

However, even Resharper suggests to convert it to a loop: Resharper suggestion

Another example, if you want to get the sum of a list you could do:

public static int Sum(List<int> summands) => summands.Count > 0
    ? summands.First() + Sum(summands.Skip(1).ToList())
    : 0;

But those examples aren't useful in C#, as C# isn't a functional programming language, which causes recursion to be slower than loops. Furthermore recursion often causes a StackOverflowException (fitting to this site). If you run the endless loop recursion, it doesn't even take a second till your stack is full.

The reason for this is, that C# adds the address, from which a method got called, to the stack. If a method is called very often (and in 1 second a lot of recursive calls are made) a lot of addresses are added to the stack, so that it overflows.

However I still think, even though those examples aren't useful in c#, that it's quite useful to be able to handle recursion. Recursion is for example the only way to explore a directory structure, for getting for example all files:

public static List<FileInfo> GetAllFiles(DirectoryInfo directory) => directory.GetFiles()
    .Concat(directory.GetDirectories().SelectMany(GetAllFiles))
    .ToList();

And, as you experienced, it's the only way to fill a nested class from a list properly.

MetaColon
  • 2,895
  • 3
  • 16
  • 38