Read the definition of median:
If the data set has an even number of observations, there is no distinct middle value and the median is usually defined to be the arithmetic mean of the two middle values
Your code doesn't do that: it takes two values which are outside the dataset, and averages them to get a result. Indexes are zero based, so valid indexes for a collection of four items are 0, 1, 2, and 3 only - the length of the collection - 4 - is not a valid index.