How to feed several (0-n) functions into another function

Hello,
I’ve got a question about how I can feed several functions (with arguments) into another function. I assume this has to work via delegates somehow, but I’m not sure how.

The basic idea is I want to have an unmark function that I can feed other functions such as “Remove buildings” or “build buildings” or just null (in case that I don’t actually want to do anything than to remove all my markings).

Currently it looks like this:

if (tool == Tool.Remove)
        {
            for (int x = xMin; x <= xMax; x++)
            {
                for (int z = zMin; z <= zMax; z++)
                {
                    Mark(x, z, 0);
                    Destroy(gameObjects[x, markingOld.y, z]);
                    fieldInfos[x, markingOld.y, z].construction = Construction.Empty;
                }
            }
        }

What I want to do is to feed the

Destroy(gameObjects[x, markingOld.y, z]);
fieldInfos[x, markingOld.y, z].construction = Construction.Empty;

into a function like this:

private void UnmarkAll(Delegate function)
    {
        for (int x = xMin; x <= xMax; x++)
        {
            for (int z = zMin; z <= zMax; z++)
            {
                Mark(x, z, 0);
                //Execute everything I fed into the argument
                function();
            }
        }
    }

So I don’t need to spam the for loops everywhere and keep my code somewhat compact.

I’d also like the option to feed a null into the “UnmarkAll” function. Or just one function. Is there a way to do this?

If you’re comfortable with a fairly unsightly syntax, you can use a lambda function cast as System.Action:

edit: added arguments, didn’t see that you needed them.

private void UnmarkAll(System.Action<int, int> action)
{
    if (tool == Tool.Remove)
    {
        for (int x = xMin; x <= xMax; x++)
        {
            for (int z = zMin; z <= zMax; z++)
            {
                Mark(x, z, 0);
                action(x, z);
            }
        }
    }
}

You’d then call it like this:

this.UnmarkAll((x, z)=>{
    Destroy(gameObjects[x, markingOld.y, z]);
    fieldInfos[x, markingOld.y, z].construction = Construction.Empty;
});