Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 42 / 68

Flattening a list

This lesson covers flattening a nested list in VB.NET, with the aim of understanding recursive processing of a data structure.

Flattening turns a list containing further lists into a single, one-level list. When you do not know how deep the nesting goes, a recursive function handles any depth.

In the sample code, a Flatten() function tests whether an element is a list with TypeOf item Is List(Of Object), calls itself to expand it when it is, and adds it to the result when it is not. Note result.AddRange(), which adds several values at once.

A common early stumble is testing the element's type. With a list that can hold anything, such as List(Of Object), you have to decide at run time whether the contents are a number or another list.

In professional work, flattening is used whenever hierarchical data — a nested menu, a folder structure, JSON — has to be processed as one flat list.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)