Populate 2D array from a particular coordinate

时间:2018-07-24 10:16:54

标签: arrays optimization

I am trying to populate a 2D array vertically or horizontally from a given coordinate, if the indexes are in range. Below is the program I can think about as of now.

I just want to know if there is any other optimized way to perform this logical operation.

public static void Main()
{
    var arr = new string[10,10];
    int x = 0;
    int y = 1;
    int len = 5;
    string somevalue = "x";
    string align = "vertical";

    int i = x;
    int j = y;

    try
    {
        while(len > 0)
        {
            arr[i,j] = somevalue;

            if(align == "vertical") 
                j++;
            else
                i++;

            len--;
        }
    }
    catch(IndexOutOfRangeException ex)
    {
        Console.WriteLine(ex);
    }
}

0 个答案:

没有答案