Why can not I print this string
in reverse? I know there is a possibility of using a simpler form, but I want to understand the error.
static void Main(string[] args)
{
string frase = "Diego lima de Aquino";
for(int i = frase.Length; i>=0; i--)
{
Console.WriteLine(frase[i]);
}
Console.ReadKey();
}
Error:
System.IndexOutOfRangeException was unhandled HResult=-2146233080 Message=Index was outside the bounds of the array. Source=mscorlib StackTrace: at System.String.get_Chars(Int32 index) at AppNoPrompt_1.Program.Main(String[] args) in C:\Users\Diegolaquino\documents\visual studio 2015\Projects\AppNoPrompt_1\AppNoPrompt_1\Program.cs:line 20 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: