title: C# Check for neighbours
body: <p>I have a function to check neighbors of an array and if that element is equal with 1. X is for each neighbor found and v[l] is the position for each 0. I have a problem with this code each time gives me "Index was outside the bounds of the array" and i don't know what to do else.</p>
  
  <pre><code> public int modificari(int i,int j,int n,int m)
      {
          int x = 0;
          v = new int[n];
          l=0;
          if (mat[i, j] == 1)
          {
              if (j++ &lt; m)
              {
                  if (mat[i, j++] == 1)
                      x++;
                  else
                  {
                      v[l] = i * n + j + 2;
                      l++;
                  }
              }
              if (j++ &lt; m &amp;&amp; i++ &lt; n)
              {
                  if (mat[i++, j++] == 1)
                      x++;
                  else
                  {
                      v[l] = (i + 1) * n + j + 2;
                      l++;
                  }
              }
              if (i++ &lt; n)
              {
                  if (mat[i++, j] == 1)
                      x++;
                  else
                  {
                      v[l] = (i + 1) * n + j + 1;
                      l++;
                  }
              }
              if (j-- &gt;= 0 &amp;&amp; i++ &lt; n)
              {
                  if (mat[i++, j--] == 1)
                      x++;
                  else
                  {
                      v[l] = (i + 1) * n + j;
                      l++;
                  }
              }
              if (j-- &gt;= 0)
              {
                  if (mat[i, j--] == 1)
                      x++;
                  else
                  {
                      v[l] = i * n + j;
                      l++;
                  }
              }
              if (j-- &gt;= 0 &amp;&amp; i-- &gt;= 0)
              {
                  if (mat[i--, j--] == 1)
                      x++;
                  else
                  {
                      v[l] = (i - 1) * n + j;
                      l++;
                  }
              }
              if (i-- &gt;= 0)
              {
                  if (mat[i--, j] == 1)
                      x++;
                  else
                  {
                      v[l] = (i - 1) * n + j + 1;
                      l++;
                  }
              }
              if (j &lt; n &amp;&amp; i-- &gt;= 0)
              {
                  if (mat[i--, j++] == 1)
                      x++;
                  else
                  {
                      v[l] = (i - 1) * n + j + 2;
                      l++;
                  }
              }
              if (x &lt; 2 &amp;&amp; x &gt; 3)
                  return 1;
              else
                  return random();
          }
          return x;
      }
  </code></pre>