'How can I skip loop for first and last element of array and set them to constant value?

I would like the loop to start from the second element and to end before the last one. So that I can set the first and last one to constant value.

for (int i = 0; i < n; i++) {
  for (int j = 0; j < 3; j++) {
    Console.Write($"A[{i},{j}] = ");
    tab[i, j] = double.Parse(Console.ReadLine());
  }
}


Solution 1:[1]

You can use LINQ simply:

var result = yourArray.Skip(1).SkipLast(1).Prepend(theConstValue)
                      .Append(theConstValue).ToArray();

The Prepend, Adds a value to the beginning of the sequence. The Append, Appends a value to the end of the sequence.

Solution 2:[2]

Why don't do something like that?

for (int i = 1; i < n - 1; i++)
{
  //your code
}

Solution 3:[3]

Let the loop start at element 1 and end it one earlier

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Nick Farsi
Solution 3 Patricia Heimfarth