C
C#12mo ago
ImBored

✅ C sharp help static void

Hello everyone I am new to C sharp and I was wondering if someone can help me with my code it says theres an error but I dont know how to fix the error can somone please help me
7 Replies
Angius
Angius12mo ago
Console.ReadLine() returns a string? so a string value that can also be null In an event where it is null, you can't .ToLower() it The most proper way to deal with it would be using a null-coalescing operator, for example
var text = Console.ReadLine()?.ToLower() ?? "";
var text = Console.ReadLine()?.ToLower() ?? "";
ImBored
ImBored12mo ago
So I should replace line 35 with var text = Console.ReadLine()?.ToLower() ?? "";
Angius
Angius12mo ago
A simpler but less idiomatic way would be the null-forgiving operator that basically says "trust me bro, this is not null". But if it does happen to be null, well, you're out of luck and your app breaks:
var text = Console.ReadLine()!.ToLower();
var text = Console.ReadLine()!.ToLower();
Yes
ImBored
ImBored12mo ago
Okay tysm
Angius
Angius12mo ago
Well, in your case the variable is not named text but guess But, yeah
ImBored
ImBored12mo ago
Thank you so much for the help
Accord
Accord12mo ago
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.