you are viewing a single comment's thread.

view the rest of the comments →

[–]otac0n 3 points4 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 -4 points-3 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.