So I just done it as a habit now but I want to know how it really works. Console.ReadLine()?.ToLower() ?? "";
So why do you put a ? in between .ToLower()
and Console.ReadLine()
. I know that after the .ToLower()
you put ?? "";
to make a null input into a string. But what about the other question mark?
Asked
Active
Viewed 170 times
-4

Alivegamer
- 15
- 3
-
1thats a null conditional – ΦXocę 웃 Пepeúpa ツ Aug 08 '22 at 13:55
-
3in case `Console.ReadLine()` returns null calling `.ToLower()` wont throw a null reference exception but instead it will return null – Rand Random Aug 08 '22 at 13:55
-
1In this particular case this is almost certainly an error -- `Console.ReadLine()` will return `null` if and only if input was redirected and no more lines are available, which is a condition we should want to detect separately, not treat as an empty string (since, if we *are* reading from redirected input, an empty line is probably not intended to end processing). – Jeroen Mostert Aug 08 '22 at 13:57
-
@seth For some reason that didn't popup when I typed it in the search bar but yes it does answer my question and explains it. – Alivegamer Aug 08 '22 at 13:57
1 Answers
0
Single question mark (?) means null check.
Basically, if Console.ReadLine is null, the other part will not be implemented.
In other words, Console.ReadLine()?.ToLower()
is equal to this
var dummy = Console.ReadLine();
if(dummy != null)
{
dummy.ToLower()....
}

ProgrammingLlama
- 36,677
- 7
- 67
- 86

ahmet gül
- 193
- 1
- 11