This is one of my questions from an interview. The interviewer asked me to rotate a square matrix in C#. While it is not so hard for me and I have done it in 10 minutes, I'd share my solution to all developers who may need this.

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = new int[][]
            {
                new int[]{ 1, 2 , 3, 4},
                new int[]{ 5, 6, 7, 8},
                new int[]{ 9, 10, 11 ,12 },
                new int[]{ 13,14,15,16 }
            };
            Rotate(ref a);
        }

        static void Rotate(ref int[][] input, int padding = 0)
        {
            for (int i = padding; i < input.Length - padding - 1; i++)
            {
                // rotate the first line;
                RotateItem(ref input, i, padding);
            }
            if (input.Length - 2 * padding >= 2)
            {
                Rotate(ref input, padding + 1);
            }
        }

        static void RotateItem(ref int[][] input, int x, int y)
        {
            var length = input.Length - 1;
            var current = input[x][y];
            var targtsTargetsTarget = input[length - y][x];
            var targetsTarget = input[length - x][length - y];
            var target = input[y][length - x];

            input[x][y] = targtsTargetsTarget;
            input[y][length - x] = current;
            input[length - x][length - y] = target;
            input[length - y][x] = targetsTarget;
        }
    }
}