C
C#2y 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
Angius2y ago
Console.ReadLine() returns string? not string
Ronnie
RonnieOP2y ago
what?
Angius
Angius2y ago
What I said
Ronnie
RonnieOP2y ago
i dont understand
Angius
Angius2y ago
The method returns a square peg You're trying to squeeze it through a round hole
Ronnie
RonnieOP2y ago
how do i fix this?
Angius
Angius2y ago
Two ways
string? name = Console.ReadLine();
string? name = Console.ReadLine();
or
string name = Console.ReadLine() ?? "";
string name = Console.ReadLine() ?? "";
Ronnie
RonnieOP2y ago
oh never seen this before lol
Angius
Angius2y ago
a ?? b means "use a, unless it's null, in that case use b"
Ronnie
RonnieOP2y ago
oh ok does user input work with the debug console in VSCode
Angius
Angius2y ago
Why wouldn't it?
Ronnie
RonnieOP2y ago
still didnt work :/
Angius
Angius2y ago
What didn't work?
Ronnie
RonnieOP2y ago
The first prompt comes up i enter a name then nothing comes up afterwards
Ronnie
RonnieOP2y ago
Angius
Angius2y ago
Odd What happens when you run it with dotnet run?
Ronnie
RonnieOP2y ago
yeah ...how do i dot that? lol
Angius
Angius2y ago
Open the console Type dotnet run Press enter
Ronnie
RonnieOP2y ago
ok
Angius
Angius2y ago
In VSCode you can open it with Ctrl+~
Ronnie
RonnieOP2y ago
that shortcut doesnt work
Thinker
Thinker2y 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
RonnieOP2y ago
nvm it works
Ⰽⰰⱈⰻⰽⱄ
If stdin is closed or eof then it returns null
Ronnie
RonnieOP2y 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
Thinker2y 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
RonnieOP2y ago
oh welp thanks for everything
Want results from more Discord servers?
Add your server