Index was outside the bounds of the array

Index was outside the bounds of the array

    Here, I need to reproduce this error and explain.

Index was outside the bounds of the array.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

  

C#.net code
  string []str = "This is Test string".Split(" ".ToCharArray());

Response.Write(str[4]);


VB.net code

Dim str As String() = "This is Test string".Split(" ".ToCharArray())

Response.Write(str(4))


'=======================================================
'Service provided by Telerik (
www.telerik.com)
'Conversion powered by NRefactory.
'Built and maintained by Todd Anglin and Telerik
'=======================================================


If you run above code you will get an error "Index was outside the bounds of the array"

why ? Because we are trying to access array elements which are not present in array.

String used here contains only 4 words, Array indexing start from 0.

Satalaj

Tags:

Linq Convert String Array To Int Arrayc# arraylistOne dimensional array example C#.net VB.netHow To Declare and access array of string in C#C# Char ArrayAspnet Listbox Selectedindexchanged problems and solutionC# Convert-List To Arraymeta index followC# Arraylist foreachC# looping through arrayC# ArrayList Search using Linq Extension OfType Method

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.