any thoughts why this error ?
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
at inclass7.exercise7_1.Main(String[] args) in /Users/user/EXSAM/Program.cs:line 17
31 Replies
i <= days.Length
Means run till i equals the length of days array
arrays in c# start at 0if you count from 0 to n, that's total (n+1) numbers
so how to remove the error
Do you know how for loops work?
foreach (var i in Enumerable.Range(0, days.Length)
or for (var i = 0; i < days.Length; i++)
hfπ
They'll just copy paste it and won't understand why that's the solution
true π
i swear i found why before i read lol
just now after i keep thinking of this
i did this
yes u ever explain to me
there is another thing actually i wana ask if that is ok
Yeah sure go ahread
there is weird yellow warning
teacher she keep saying ignore it
Itβs appear in readline
What's the warning?
you do
double.Parse(Console.ReadLine())
?yessssssss
use
Convert.ToDouble(Console.ReadLine())
there is a difference between them if you hover itcan i understand actually why
double.Parse
has an additional ArgumentNullException β s is null.
that's whyWell, the reason you get this warning is because Console.ReadLine can return null
double.Parse doesn't work with null
i donβt really understand null
u mean like return value?
Console.ReadLine may be null
null means no reference
ohhhh
then what is the real use of parse
To parse a string
to try parse to int or something
there are two patterns
they are referred as Tester-Doer
void Parse(...)
and bool TryParse(..., out ...)
Exceptions and Performance - Framework Design Guidelines
Learn more about: Exceptions and Performance
I don't think they were taught the
out
key word π
me neither
but good to know
i am a bit lost to be honest
if something goes sideways in
Parse
, it's supposed to throw an exception such as InvalidOperationException. TryParse
however try/catch'es it, so it never throws and instead it returns a boolean
let's say you're parsing an .xls document or something
and u need to know if a cell is an integer
you would do bool isInteger = int.TryParse(cell, out _)
did you understand it that way?
in your case it's trying to parse the string you write in the console to double