❔ string with error onverting null literal or possible null value to non-nullable type.
i think the string isnt workijng and wen i use var it works but the only thing that isnt working is the varaible on the readline isnt functionng right
7 Replies
Console.ReadLine()
returns a string?
not a string
It's due to an edge case when certain inputs can result in it not returning a string, not even an empty one, but rather returning nothing
A common workaround is Console.ReadLine() ?? ""
It provides a fallback, in case the readline returns null
, it falls back to ""
, an empty string, so it always remains a stringthank u
so much
but
does this issue happen with like
int bool and float values
and if it does would i use the same fix
Depends on what any given method returns
It's not something related to strings, or bools, or ints
It's something related to nullable types
A method can return
int
or it can return int?
It can return string?
like this one, or it can return string
ok this is all good 2 know
thx
In case of nullable types, yeah, you can always use a fallback
nullableInt ?? 0
, nullableBool ?? false
, nullableChar ?? '\0'
etcok thx
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.