<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged bounds</title>
<link>https://coderlegion.com/tag/bounds</link>
<description>Powered by Question2Answer</description>
<item>
<title>Index was outside the bounds of the array</title>
<link>https://coderlegion.com/18567/index-was-outside-the-bounds-of-the-array</link>
<description>&lt;p&gt;&lt;strong style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Today, we're going to fix a C# Error: &lt;span style=&quot;color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Index was outside the bounds of the array&lt;/em&gt;&lt;/span&gt; that usually occurs when we try to access the &lt;em&gt;index less than 0&lt;/em&gt; or &lt;em&gt;greater than or equal to the array length - 1&lt;/em&gt;. In this study blog, we'll explore why it happens and how to resolve it.&lt;/strong&gt;&lt;/p&gt;
&lt;div id=&quot;toc-div&quot;&gt;
  &lt;h4&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt; Table of Contents: &lt;a id=&quot;toc-toogle&quot; class=&quot;open&quot;&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h4&gt;
  &lt;div id=&quot;toc-list&quot;&gt;
    &lt;ol&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774610&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;What is &quot;Index was outside the bounds of the array&quot;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774611&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Causes&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774612&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Solution&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774613&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;The Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#_Toc123774614&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;The Reference&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774610&quot; id=&quot;_Toc123774610&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;What is &quot;Index was outside the bounds of the array&quot;&lt;/span&gt; &lt;a id=&quot;TypeError-str-object&quot; style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774610&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      This error happens when we try to access the elements in the array using &lt;em&gt;&lt;strong&gt;invalid index&lt;/strong&gt;&lt;/em&gt;. Let's see an example:
    &lt;/span&gt;&lt;/p&gt;

  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9 };
for(int i = 0; i &amp;lt;= numbers.Length; i++)
{
    Console.WriteLine(numbers[i]);
}
  &amp;lt;/span&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      This is the output:
    &lt;/span&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
1
2
3
4
5
6
7
8
9
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      Why? Because the &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;numbers&lt;/em&gt; array has 9 elements, and thus its length is 9. However, in programming, each collection like array has indices in the range of &lt;em&gt;&lt;strong&gt;[0 - N-1], where N = array.length&lt;/strong&gt;&lt;/em&gt; and we can access each element in the array within that range. In the above example, after accessing &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;index 8 with element 9&lt;/em&gt; in &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;numbers&lt;/em&gt; array, we tend to access element at index &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;9&lt;/em&gt;, which is an &lt;em&gt;invalid index&lt;/em&gt; for &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;numbers&lt;/em&gt; array. That's why this error occur.
    &lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      Let's look at another example:
    &lt;/span&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9 };
numbers[-1] = 10;
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      This is the output:
    &lt;/span&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      Why? we're trying to assign value to the element of &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;numbers&lt;/em&gt; array at index &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;-1&lt;/em&gt;, which is also an &lt;em&gt;invalid index&lt;/em&gt; for &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;numbers&lt;/em&gt; array. Hence, the error will also occur.
    &lt;/span&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774611&quot; id=&quot;_Toc123774611&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Causes&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774611&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      The error, &lt;span style=&quot;color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Index was outside the bounds of the array&lt;/em&gt;&lt;/span&gt; is actually under &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;IndexOutOfRangeException&lt;/em&gt; Class in C#. &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;IndexOutOfRangeException: The exception is thrown when the attempt to access the element of a collection or an array using the index outside its bounds is made.&lt;/em&gt; Hence, &lt;span style=&quot;color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'&lt;/em&gt;&lt;/span&gt; occurs when we're trying to access the element of an array using &lt;em&gt;&lt;strong&gt;invalid index&lt;/strong&gt;&lt;/em&gt;. Don't fret; it can be simply resolved.
    &lt;/span&gt;&lt;/p&gt;
  &lt;div style=&quot;margin-top: 15px&quot; class=&quot;div-purple&quot;&gt;
    &lt;span class=&quot;alert-header&quot;&gt;Note&lt;/span&gt;
    &lt;span class=&quot;alert-body&quot;&gt;Every collection such as list, array or the other has only indices in the range of &lt;em&gt;[0, N-1]&lt;/em&gt;, where &lt;em&gt;N is the length of the array&lt;/em&gt; itself.&lt;/span&gt;
  &lt;/div&gt;
&lt;/section&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774612&quot; id=&quot;_Toc123774612&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;Solution&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774612&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      The solution is pretty simple, we just need to ensure we're accessing the elements of the array using the indices within its bounds. Let's say:
    &lt;/span&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9 };
for(int i = 0; i &amp;lt; numbers.Length; i++)
{
    Console.WriteLine(numbers[i]);
}
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      This is the output:
    &lt;/span&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
1
2
3
4
5
6
7
8
9
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      or you may also do like this:
    &lt;/span&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9 };
for(int i = 0; i &amp;lt;= numbers.Length-1; i++)
{
    Console.WriteLine(numbers[i]);
}
  &amp;lt;/span&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      This is the output:
    &lt;/span&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
1
2
3
4
5
6
7
8
9
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      Both examples above are having the same output. What we do is to ensure that the iteration will only be in the range of &lt;em&gt;&lt;strong&gt;[0 - N-1], where N = array.length&lt;/strong&gt;&lt;/em&gt;. The &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;numbers&lt;/em&gt; array has the length of 9, and thus can only iterate from 0 to 8. These are the &lt;em&gt;&lt;strong&gt;valid indices&lt;/strong&gt;&lt;/em&gt; for the &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;numbers&lt;/em&gt; array.
    &lt;/span&gt;&lt;/p&gt;
  &lt;div style=&quot;margin-top: 15px&quot; class=&quot;div-green&quot;&gt;
    &lt;span class=&quot;alert-header&quot;&gt;Tip&lt;/span&gt;
    &lt;span class=&quot;alert-body&quot;&gt;You may also use &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;GetLowerBound(Int32)&lt;/em&gt; and &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;GetUpperBound(Int32)&lt;/em&gt; to avoid this error.&lt;/span&gt;
  &lt;/div&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
int[] numbers = new int[] { 1,2,3,4,5,6,7,8,9 };
for(int i = numbers.GetLowerBound(0); i &amp;lt;= numbers.GetUpperBound(0); i++)
{
    Console.WriteLine(numbers[i]);
}
  &amp;lt;/span&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      This is the output:
    &lt;/span&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;
1
2
3
4
5
6
7
8
9
  &lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      For the case above, &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;GetLowerBound(0)&lt;/em&gt; is used to get the &lt;em&gt;&lt;strong&gt;index of the first element&lt;/strong&gt;&lt;/em&gt;, which is &lt;em&gt;0&lt;/em&gt; while &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;GetUpperBound(0)&lt;/em&gt; is used to get the &lt;em&gt;&lt;strong&gt;index of last element&lt;/strong&gt;&lt;/em&gt;. Hence, we can omit the error by using these two functions together.
    &lt;/span&gt;&lt;/p&gt;
  &lt;div style=&quot;margin-top: 15px&quot; class=&quot;div-purple&quot;&gt;
    &lt;span class=&quot;alert-header&quot;&gt;Note&lt;/span&gt;
    &lt;span class=&quot;alert-body&quot;&gt;The &lt;em&gt;dimension&lt;/em&gt; for both &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;GetLowerBound(int dimension)&lt;/em&gt; and &lt;em style=&quot;background-color: rgb(206, 212, 217); color: rgb(0, 0, 0);&quot;&gt;GetUpperBound(int dimension)&lt;/em&gt; is determined by the array type, whether it is &lt;em&gt;one-dimensional array&lt;/em&gt; or &lt;em&gt;two-dimensional array&lt;/em&gt; as they have the different &lt;a rel=&quot;nofollow&quot; href=&quot;https://learn.microsoft.com/en-us/dotnet/api/system.array.rank?view=net-7.0#system-array-rank&quot; target=&quot;_blank&quot;&gt;Rank&lt;/a&gt;. Make sure the dimension has the value of &lt;strong&gt;&lt;em&gt;greater than 0&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;Rank - 1&lt;/em&gt;&lt;/strong&gt;.&lt;/span&gt;
  &lt;/div&gt;
&lt;/section&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774613&quot; id=&quot;_Toc123774613&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;The Conclusion&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774613&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      In this study blog, we've explore what is &lt;span style=&quot;color: rgb(185, 106, 217);&quot;&gt;&lt;em&gt;Index was outside the bounds of the array&lt;/em&gt;&lt;/span&gt; in C#, why it happens and how to resolve it. You should always be aware with the indices of the array when you want to access its element. Make sure not to access index outside of its bound, which is the range &lt;em&gt;&lt;strong&gt;[0 - N - 1], where N = length of the array&lt;/strong&gt;&lt;/em&gt;. Thanks!
    &lt;/span&gt;&lt;/p&gt;
&lt;/section&gt;
&lt;section&gt;
  &lt;h2&gt;&lt;a class=&quot;custom_anchor&quot; name=&quot;_Toc123774614&quot; id=&quot;_Toc123774614&quot;&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;The Reference&lt;/span&gt; &lt;a style=&quot;color: blue;&quot; rel=&quot;nofollow&quot; href=&quot;#_Toc123774614&quot;&gt;#&lt;/a&gt;&lt;/strong&gt;&lt;/h2&gt;
  &lt;p&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;
      For further information, please visit:
    &lt;/span&gt;&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;&lt;em&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;IndexOutOfRangeException: &lt;/span&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://learn.microsoft.com/en-us/dotnet/api/system.indexoutofrangeexception?view=net-7.0&quot; target=&quot;_blank&quot;&gt;https://learn.microsoft.com/en-us/dotnet/api/system.indexoutofrangeexception?view=net-7.0&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;GetLowerBound(Int32) in LINQ: &lt;/span&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://learn.microsoft.com/en-us/dotnet/api/system.array.getlowerbound?view=net-7.0#system-array-getlowerbound(system-int32)&quot; target=&quot;_blank&quot;&gt;https://learn.microsoft.com/en-us/dotnet/api/system.array.getlowerbound?view=net-7.0#system-array-getlowerbound(system-int32)&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;&lt;span style=&quot;font-family: arial, helvetica, sans-serif;&quot;&gt;GetUpperBound(Int32): &lt;/span&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://learn.microsoft.com/en-us/dotnet/api/system.array.getupperbound?view=net-7.0#system-array-getupperbound(system-int32)&quot; target=&quot;_blank&quot;&gt;https://learn.microsoft.com/en-us/dotnet/api/system.array.getupperbound?view=net-7.0#system-array-getupperbound(system-int32)&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
  &lt;/ol&gt;
&lt;/section&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/18567/index-was-outside-the-bounds-of-the-array</guid>
<pubDate>Thu, 21 May 2026 12:28:19 +0000</pubDate>
</item>
</channel>
</rss>