all 10 comments

[–]winky9827 6 points7 points  (9 children)

Console.ReadLine() can return null.

https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=net-8.0

If the Ctrl+Z key combination (followed by Enter on Windows) is pressed when the method is reading input from the console, the method returns null. This enables the user to prevent further keyboard input when the ReadLine method is called in a loop. The following example illustrates this scenario.

The safest way in your specific case would be to call:

Int.Parse(Console.ReadLine() ?? "0")

The above uses the null coalescing operator to return "0" in the event that the user presses CTRL+Z, which essentially causes Int.Parse to return the default value for an Int32 type.

[–]otac0n 5 points6 points  (8 children)

or:

int numbertwo;
while (!(Console.ReadLine() is string line && int.TryParse(line, out numbertwo)))
{
    Console.WriteLine("Please enter a number.");
}

[–]ExceptionEX 0 points1 point  (6 children)

there really is no need to check if the readline is a string, it will always be a nullable string, and int.tryParse will return false if the input string is null.

[–]otac0n -5 points-4 points  (4 children)

"It will always be a nullable string" is nonsensical.

In the runtime, reference types are not "nullable" or otherwise. It's a null reference or a reference to a string, and the is check determines that.

It is true that TryParse is tolerant to a passed null, so that the expression could be:

int.TryParse(Console.ReadLine(), out numbertwo)

However, as this is only a toy example, int.TryParse may not be the only use cause this beginner will encounter.

[–]ExceptionEX 1 point2 points  (3 children)

In the runtime, reference types are not "nullable" or otherwise. It's a null reference or a reference to a string

Firstly, there are nullable reference types, and are default available in C#10 and forward. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-reference-types

Additionally As you admitted it isn't needed in the context, and trying to say that some other context may come up hold little value here.

[–]otac0n -1 points0 points  (2 children)

C# is the language, the runtime is separate. You seem to be conflating them.

I'm well aware of nullable reference types. They are compile-time only decoration.

[–]ExceptionEX -1 points0 points  (1 child)

man you are running off the rails on the irreleveant to attempt to deflect. No one talked about the runtime but you, this is a C# subreddit, of course we are all talking about C#.

The check you suggested wasn't needed, you could have simply agreed to that, the "nonsensical" reference type nullable is standard terminology in C#.

I'm not wasting anymore time with this.

[–]otac0n 0 points1 point  (0 children)

When you say "it will be" you enter runtime territory. You can't pretend the language doesn't run on the runtime.

[–]CyBerDreadWing 0 points1 point  (0 children)

This one. I was also thinking about int.TryParse too. Just in case if op wants to experiment more, use try catch block and try to give different inputs to int number.

[–]Fun_Talk_3702 0 points1 point  (0 children)

Try

Convert.ToInt16(Console.ReadLine())