LeetCode: Spiral Matrix II C# https://leetcode.com/problems/spiral-matrix-ii/
Please review for performance, how can this code run faster


  Given a positive integer n, generate a square matrix filled with
  elements from 1 to n^2 in spiral order.
  
  Example:

Input: 3
Output:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]



using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArrayQuestions
{

    /// &lt;summary&gt;
    /// https://leetcode.com/problems/spiral-matrix-ii/
    /// &lt;/summary&gt;
    [TestClass]
    public class SpiralMatrix2Test
    {
        [TestMethod]
        public void Example3x3Test()
        {
            int size = 3;
            int[][] expected =
            {
                new[]{1,2,3},
                new [] {8,9,4},
                new []{7,6,5}
            };
            int[][] res= SpiralMatrix2.GenerateMatrix(size);
            for (int i = 0; i &lt; size; i++)
            {
                CollectionAssert.AreEqual(expected[i],res[i]);
            }
        }
}

    public class SpiralMatrix2
    {
        public static int[][] GenerateMatrix(int n)
        {

            int startCol = 0;
            int endCol = n - 1;
            int startRow = 0;
            int endRow = n - 1;
            int num = 1;
            int[][] res = new int[n][];
            for (int i = 0; i &lt; n; i++)
            {
                res[i] = new int[n];
            }

            if (n == 0)
            {
                return res;
            }

            while (startCol &lt;= endCol &amp;&amp; startRow &lt;= endRow)
            {
                for (int i = startCol; i &lt;= endCol; i++)
                {
                    res[startRow][i] = num++;
                }
                startRow++;// already did this row skip to the next one

                for (int i = startRow; i &lt;= endRow; i++)
                {
                    res[i][endCol] = num++;
                }

                endCol--;// already did the last col move back one col

                for (int i = endCol; i &gt;= startCol; i--)
                {
                    //keep in mind this is a spiral we can be in a line we are not suppose to touch the values
                    //only in the upper half of the matrix we need
                    if (startRow &lt;= endRow)
                    {
                        res[endRow][i] = num++;
                    }
                }
                endRow--;


                for (int i = endRow; i &gt;= startRow; i--)
                {
                    //we need to print the numbers only in the left half of the matrix
                    if (startCol &lt;= endCol)
                    {
                        res[i][startCol] = num++;
                    }
                }

                startCol++;
            }
            return res;

        }
    }
}
If n is zero then the loop

while (startCol &lt;= endCol &amp;&amp; startRow &lt;= endRow)


does nothing, which means that the test

if (n == 0)


is not necessary.

Here

for (int i = endCol; i &gt;= startCol; i--)
{
    //keep in mind this is a spiral we can be in a line we are not suppose to touch the values
    //only in the upper half of the matrix we need
    if (startRow &lt;= endRow)
    {
        res[endRow][i] = num++;
    }
}


the condition of the if-statement is a loop invariant, so that it can be done outside of the loop:

if (startRow &lt;= endRow)
{
    for (int i = endCol; i &gt;= startCol; i--)
    {
        res[endRow][i] = num++;
    }
}


Even if the C# compiler is smart enough to recognize the loop invariant and reorders the statements, the latter variant would be clearer to the reader of your code. The same applies to the next loop in your function.

Then note that startRow/startCol and endRow/endCol have the same value at the start of the loop body, and the tests 

if (startRow &lt;= endRow) ...
if (startCol &lt;= endCol) ...


can fail only in the last iteration. Therefore one can move that case out of the main loop, use only two variables for the first and last row/column, and write the inner loops in a symmetric fashion:

int start = 0;
int end = n - 1;

while (start &lt; end)
{
    for (int i = start; i &lt; end; i++)
    {
        res[start][i] = num++;
    }
    for (int i = start; i &lt; end; i++)
    {
        res[i][end] = num++;
    }
    for (int i = end; i &gt; start; i--)
    {
        res[end][i] = num++;
    }
    for (int i = end; i &gt; start; i--)
    {
        res[i][start] = num++;
    }

    start++;
    end--;
}

if (start &lt;= end)
{
    res[start][start] = num;
}