C
C#4d ago
Faker

Taking user input using Console.ReadLine()

Hello guys, can someone explain why my IDE (rider) has underlined the Console.ReadLine method pls.
C#
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.WriteLine(name);
C#
Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.WriteLine(name);
What is wrong here? what can be corrected and why pls
No description
11 Replies
asdf
asdf4d ago
The function returns null if stdin is closed
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
asdf
asdf4d ago
Though it's pretty rare so for a basic it CLI you can just ignore it by adding a ! Console.ReadLine()!
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP4d ago
yeah just test that I thought, null was refering to like if we just press enter when we don't write anything but in fact when we don't write anything, we just have an empty string what does the ! do pls
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP4d ago
using ! is like !important symbol in CSS
asdf
asdf4d ago
I think a better Console.ReadLine API would just throw an EndOfStream exception
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP4d ago
by the way I just switch from string to var, it seems the keyword was no longer underlined, is that a reason for that? Also, when should we use the var keyword? each time we declare a variable that we know we would be assigning something to it? ah the thing is when we use var, we are telling the compiler that we will always expect something to be entered? it works just like the ! thing I think
Tvde1
Tvde14d ago
You have the option to write down the type, or to type var and let the compiler pick the type based on the what the function returns Some people have different tastes, but the majority of people use var whenever possible

Did you find this page helpful?