C#
C#

help

Root Question Message

Hercules_x_x
Hercules_x_x10/28/2022
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
Kouhai
Kouhai10/28/2022
i <= days.Length
Means run till i equals the length of days array
arrays in c# start at 0
ν•œμš°
ν•œμš°10/28/2022
if you count from 0 to n, that's total (n+1) numbers
Hercules_x_x
Hercules_x_x10/28/2022
so how to remove the error
Kouhai
Kouhai10/28/2022
Do you know how for loops work?
Electron
Electron10/28/2022
foreach (var i in Enumerable.Range(0, days.Length)
Electron
Electron10/28/2022
or for (var i = 0; i < days.Length; i++)
Electron
Electron10/28/2022
hf
Kouhai
Kouhai10/28/2022
πŸ˜… They'll just copy paste it and won't understand why that's the solution
Electron
Electron10/28/2022
true πŸ˜„
Hercules_x_x
Hercules_x_x10/28/2022
i swear i found why before i read lol
Hercules_x_x
Hercules_x_x10/28/2022
just now after i keep thinking of this
Hercules_x_x
Hercules_x_x10/28/2022
i did this
Hercules_x_x
Hercules_x_x10/28/2022
yes u ever explain to me
Hercules_x_x
Hercules_x_x10/28/2022
there is another thing actually i wana ask if that is ok
Kouhai
Kouhai10/28/2022
Yeah sure go ahread
Hercules_x_x
Hercules_x_x10/28/2022
there is weird yellow warning
Hercules_x_x
Hercules_x_x10/28/2022
teacher she keep saying ignore it
Hercules_x_x
Hercules_x_x10/28/2022
It’s appear in readline
Kouhai
Kouhai10/28/2022
What's the warning?
Electron
Electron10/28/2022
you do double.Parse(Console.ReadLine())?
Hercules_x_x
Hercules_x_x10/28/2022
yessssssss
Electron
Electron10/28/2022
use Convert.ToDouble(Console.ReadLine())
Electron
Electron10/28/2022
there is a difference between them if you hover it
Hercules_x_x
Hercules_x_x10/28/2022
can i understand actually why
Electron
Electron10/28/2022
double.Parse has an additional ArgumentNullException – s is null.
Electron
Electron10/28/2022
that's why
Kouhai
Kouhai10/28/2022
Well, the reason you get this warning is because Console.ReadLine can return null
Kouhai
Kouhai10/28/2022
double.Parse doesn't work with null
Hercules_x_x
Hercules_x_x10/28/2022
i don’t really understand null
Electron
Electron10/28/2022
Electron
Electron10/28/2022
Hercules_x_x
Hercules_x_x10/28/2022
u mean like return value?
Electron
Electron10/28/2022
Console.ReadLine may be null
Kouhai
Kouhai10/28/2022
null means no reference
Hercules_x_x
Hercules_x_x10/28/2022
ohhhh
Hercules_x_x
Hercules_x_x10/28/2022
then what is the real use of parse
Kouhai
Kouhai10/28/2022
To parse a string
Electron
Electron10/28/2022
to try parse to int or something
Electron
Electron10/28/2022
there are two patterns
Electron
Electron10/28/2022
they are referred as Tester-Doer
Electron
Electron10/28/2022
void Parse(...) and bool TryParse(..., out ...)
Kouhai
Kouhai10/28/2022
I don't think they were taught the out key word πŸ˜…
Electron
Electron10/28/2022
public struct DateTime
{
    public static DateTime Parse(string dateTime)
    {
        ...
    }
    public static bool TryParse(string dateTime, out DateTime result)
    {
        ...
    }
}
Electron
Electron10/28/2022
me neither
Electron
Electron10/28/2022
but good to know
Hercules_x_x
Hercules_x_x10/28/2022
i am a bit lost to be honest
Electron
Electron10/28/2022
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
Electron
Electron10/28/2022
let's say you're parsing an .xls document or something
Electron
Electron10/28/2022
and u need to know if a cell is an integer
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy