C
C#ā€¢5mo ago
Mathall

possible null reference warning

C#
static string? GetName()
{
string? input = default;
bool needName = true;
while (needName)
{
Console.WriteLine("What is your name?");
input = Console.ReadLine();

if (!input.All(char.IsLetterOrDigit) || String.IsNullOrEmpty(input)) //!!!
{
Console.WriteLine("Please make sure that the name only contains letters or digits!");
}
else
{
needName = false;
}
}
return input;
}
C#
static string? GetName()
{
string? input = default;
bool needName = true;
while (needName)
{
Console.WriteLine("What is your name?");
input = Console.ReadLine();

if (!input.All(char.IsLetterOrDigit) || String.IsNullOrEmpty(input)) //!!!
{
Console.WriteLine("Please make sure that the name only contains letters or digits!");
}
else
{
needName = false;
}
}
return input;
}
No description
16 Replies
Pobiega
Pobiegaā€¢5mo ago
check the return type on Console.ReadLine
Mathall
MathallOPā€¢5mo ago
it returns a string?
Pobiega
Pobiegaā€¢5mo ago
correct: string? meaning a nullable string so it might be null and calling input.All on null would do what?
Mathall
MathallOPā€¢5mo ago
ah i see sorry if this is a dumb question, but is there any way to remove the warning
Pobiega
Pobiegaā€¢5mo ago
certainly ! input = Console.ReadLine()!; for specifically readline, this is fine, since the only time it returns null is... a very specific scenario
Mathall
MathallOPā€¢5mo ago
Thanks a ton
Pobiega
Pobiegaā€¢5mo ago
in most cases, using ! to silence a null warning is... problematic you are just hiding what can likely be an error also, this entire method can be greatly simplified, if you are interested?
Angius
Angiusā€¢5mo ago
Console.ReadLine() ?? "" would probably be more idiomatic than !
Mathall
MathallOPā€¢5mo ago
I simply put the ! directly after input on the if statement is there something major that I am missing?
Pobiega
Pobiegaā€¢5mo ago
static string GetName() // make it `string`,since we only return when its not null
{
while (true)
{
Console.WriteLine("What is your name?");
var input = Console.ReadLine();

if (string.IsNullOrWhiteSpace(input))
{
Console.WriteLine("Please enter a valid name!");
continue;
}

if (!input.All(char.IsLetterOrDigit))
{
Console.WriteLine("Please make sure that the name only contains letters or digits!");
continue;
}

return input;
}
static string GetName() // make it `string`,since we only return when its not null
{
while (true)
{
Console.WriteLine("What is your name?");
var input = Console.ReadLine();

if (string.IsNullOrWhiteSpace(input))
{
Console.WriteLine("Please enter a valid name!");
continue;
}

if (!input.All(char.IsLetterOrDigit))
{
Console.WriteLine("Please make sure that the name only contains letters or digits!");
continue;
}

return input;
}
this is how I would write this method if you dont care about giving two different error messages, you can combine them again I just prefer to split it like this but the removal of the needName and inlining the return inside the loop is... just easier
Mathall
MathallOPā€¢5mo ago
oh yeah, im pretty sure that I started this with the intention of having the input be an out string but since i changed from that this is much better
Pobiega
Pobiegaā€¢5mo ago
also, note how I didn't need to do any ! to remove the null warning since we check string.IsNullOrWhitespace šŸ™‚
Mathall
MathallOPā€¢5mo ago
ah Thanks a ton, this is really helpful
Sonath
Sonathā€¢5mo ago
the warning disappears if you start your if statement with the null check first, because it assumes that if it's true the 2nd boolean statement doesn't have to be checked.
if (string.IsNullOrEmpty(input) || !input.All(char.IsLetterOrDigit))
if (string.IsNullOrEmpty(input) || !input.All(char.IsLetterOrDigit))
but in general Pobiega's response is better, because it provides more details on the user regarding what went wrong
qqdev
qqdevā€¢5mo ago
CTRL+Z, right? Sounds like it would be worth to check for null as you did in the final solution :blobthumbsup:
Pobiega
Pobiegaā€¢5mo ago
yeah ctrl+z or if the input is redirected, so its pretty rare. I agree that its better to actually check for the null (since you can effectively treat it as "no input")
Want results from more Discord servers?
Add your server