The composite pattern is a structural design pattern that's used when you want to compose objects into tree structures to represent part-whole hierarchies.

Basically, the composite pattern is useful when we want to treat individual objects and collections of objects in the same way. For example, to represent a hierarchy of file and folders we could use the composite pattern as follows.

// Composite Item Interface
public interface IFileSystemItem
{
    int Id { get; set; }
    string Name { get; set; }
    int Size { get; }
}

// Composite Item: Folder
public class Folder : IFileSystemItem
{
    public Folder()
    {
        this.Files = new List<File>();
        this.Folders = new List<Folder>();
    }

    public int Id { get; set; }

    public string Name { get; set; }

    public int Size
    {
        get
        {
            int size = 0;

            foreach(var file in this.Files)
            {
                size += file.Size;
            }

            foreach (var folder in this.Folders)
            {
                size += folder.Size;
            }

            return size;
        }
    }

    public List<File> Files { get; set; }

    public List<Folder> Folders { get; set; }
}

// Composite Item: File
public class File : IFileSystemItem
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int Size { get; set; }
}

So using the above code we can treat individual files/folders and collections of files/folders in the same way by virtue of the IFileSystemItem interface.

If we wanted to read the size of all files in the hierarchy we could do as follows:

var rootFolder = new Folder() { Id = 1, Name = "Folder One" };
rootFolder.Files.Add(new File { Id = 1, Name = "File One", Size = 142 });
rootFolder.Files.Add(new File { Id = 2, Name = "File Two", Size = 456 });
rootFolder.Files.Add(new File { Id = 3, Name = "File Three", Size = 978 });
rootFolder.Files.Add(new File { Id = 4, Name = "File Four", Size = 183 });
rootFolder.Files.Add(new File { Id = 5, Name = "File Five", Size = 22 });

var folderTwo = new Folder { Id = 2, Name = "Folder Two" };
folderTwo.Files.Add(new File { Id = 6, Name = "File Six", Size = 98 });
folderTwo.Files.Add(new File { Id = 7, Name = "File Seven", Size = 22 });
folderTwo.Files.Add(new File { Id = 8, Name = "File Eight", Size = 233 });
rootFolder.Folders.Add(folderTwo);

var folderThree = new Folder { Id = 3, Name = "Folder Three" };
folderThree.Files.Add(new File { Id = 9, Name = "File Nine", Size = 219 });
folderThree.Files.Add(new File { Id = 10, Name = "File Ten", Size = 999 });
rootFolder.Folders.Add(folderThree);

var folderFour = new Folder { Id = 4, Name = "Folder Four" };
folderFour.Files.Add(new File { Id = 11, Name = "File Eleven", Size = 2 });
folderFour.Files.Add(new File { Id = 12, Name = "File Twelve", Size = 2 });
folderFour.Files.Add(new File { Id = 13, Name = "File Thirteen", Size = 2 });
folderFour.Files.Add(new File { Id = 14, Name = "File Fourteen", Size = 2 });
folderTwo.Folders.Add(folderFour);

// Get the total size of all elements in the hierarchy.
Console.Write(rootFolder.Size);

// Output: 3360

Therefore, as you can see we can use the composite pattern as above to iterate through all of the folders and files to get the size of all files in all folders from the root folder.