This code is about manipulating each value in a 2D array. My method below is called based on how I want the values in the array to be manipulated (process).For the first method in the switch statement - as an example, I am calling AllCaps() method to manipulate the value.What I am trying to avoid is repeating statements when I can put it in a method instead and just call it when needed. With my code below, though, it seems like I am making the program work too hard by performing a check on each iteration - which could be in the thousands.Is it effective to call a method for processing each 2D array value inside the nested for loop? Or call the method first and perform the nested for loop inside each method? private void ProcessSelectedRange(string process){    selectedRange = GetSelectedRange();    // initialize and populate 2d array with values from selected range    curValue = new object[,] { };    curValue = selectedRange.Value;    // possibly more statements    // loop through curValue 2d array    for (int i = 1; i <= curValue.GetLength(0); i++)    {        for (int j = 1; j <= curValue.GetLength(1); j++)        {            string str = string.Empty;            // continue if curValue[i, j] is null or empty            try            {                str = curValue[i, j].ToString();                if (string.IsNullOrEmpty(str)) continue;            }            catch { continue; }            //********************************************************************            // call method depending on what we want to do with the value            switch (process)            {                case AllCaps: AllCaps(str); break;                case Process2: Process2(str); break;                case Process3: Process3(str); break;                case Process4: Process4(str); break;                // possibly more methods                default: Process5(str); break;            }            //********************************************************************        }    }}
Manipulating each value in a 2D array
c#;performance
ExceptionAvoid getting them being thrown if possible, as they are costly.if (curValue[i, j] == null)    continue;var str = curValue[i, j].ToString();if (string.IsNullOrEmpty(str))    continue;LoopThe condition will be evaluated every cycle. You do not need to GetLength on each of the loop. The result can be cached : // could be named rowCount and columnCount; I am not sure which is whichvar length0 = curValue.GetLength(0), length1 = curValue.GetLength(1)// when you have 2 for-loop like that, you don't need to intend all of them// this is mostly a style choice, but it helps to prevent building a pyramidfor (int i = 1; i <= length0; i++)for (int j = 1; j <= length1; j++){   // ...}The switch part can be moved out of the loop, as the process doesn't change during the loop. This can save us from some string comparisons.Action<string> processString;switch (process){    case AllCaps: processString = AllCaps; break;    case Process2: processString = Process2; break;    case Process3: processString = Process3; break;    case Process4: processString = Process4; break;    // possibly more methods    default: processString = Process5;}for (...)for (...){    // ...    processString(str);}