C
C#16mo ago
Ronnie

✅ User input

using System;

namespace CSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is your name?: ");

string name = Console.ReadLine();

Console.WriteLine("Your username is " + name);

}
}
}
using System;

namespace CSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is your name?: ");

string name = Console.ReadLine();

Console.WriteLine("Your username is " + name);

}
}
}
Idk VScode wont print out their name in the debug console
28 Replies
Angius
Angius16mo ago
Console.ReadLine() returns string? not string
Ronnie
Ronnie16mo ago
what?
Angius
Angius16mo ago
What I said
Ronnie
Ronnie16mo ago
i dont understand
Angius
Angius16mo ago
The method returns a square peg You're trying to squeeze it through a round hole
Ronnie
Ronnie16mo ago
how do i fix this?
Angius
Angius16mo ago
Two ways
string? name = Console.ReadLine();
string? name = Console.ReadLine();
or
string name = Console.ReadLine() ?? "";
string name = Console.ReadLine() ?? "";
Ronnie
Ronnie16mo ago
oh never seen this before lol
Angius
Angius16mo ago
a ?? b means "use a, unless it's null, in that case use b"
Ronnie
Ronnie16mo ago
oh ok does user input work with the debug console in VSCode
Angius
Angius16mo ago
Why wouldn't it?
Ronnie
Ronnie16mo ago
still didnt work :/
Angius
Angius16mo ago
What didn't work?
Ronnie
Ronnie16mo ago
The first prompt comes up i enter a name then nothing comes up afterwards
Ronnie
Ronnie16mo ago
Angius
Angius16mo ago
Odd What happens when you run it with dotnet run?
Ronnie
Ronnie16mo ago
yeah ...how do i dot that? lol
Angius
Angius16mo ago
Open the console Type dotnet run Press enter
Ronnie
Ronnie16mo ago
ok
Angius
Angius16mo ago
In VSCode you can open it with Ctrl+~
Ronnie
Ronnie16mo ago
that shortcut doesnt work
Thinker
Thinker16mo ago
For context, string? means that it's either a string or null. Console.ReadLine() can in very rare circumstances return null, which is why it returns a string?.
Ronnie
Ronnie16mo ago
nvm it works
Ⰽⰰⱈⰻⰽⱄ
If stdin is closed or eof then it returns null
Ronnie
Ronnie16mo ago
oh ok i ran dotnet run in the terminal and it worked my debug console in VSCode cant take user input... weird
Ⰽⰰⱈⰻⰽⱄ
I don't know what key is in Windows, but if you press ctrl+d on macOS or Linux it will cause eof on stdin
Thinker
Thinker16mo ago
You have to set the console in your .vscode/launch.json file to integratedTerminal. The default console doesn't allow input for some odd reason.
Ronnie
Ronnie16mo ago
oh welp thanks for everything