code?
Cause I'm being taught to use ReadKey() as a way to close it
Environment.Exit(0);
no! don't do that. really bad way to stop a program.
How can you close a program with readkey?
It's about the same as killing a process. That's not clean exit. Background threads are forcibly killed, finally handlers are not executed. Exit is considered an unsafe call.
Basically having empty readkey makes it so program closes with a keypress
I tried it, it's worse than readkey as it doesn't wait for input
If there’s a return after it
Even without return
🤷♂🤷♂ In a real program yes. In sandbox, I don't care
Could you show sample code
you were litterally answering the question "What's the more proper way of closing C# program" and now you say "in sandbox I don't care" (about code quality) don't give bad advice to others
It's just Console.ReadKey(); at the end of main class
That’s what i said
When someone is asking about readKey, it means the program is some starter learning staff
There are 2 type of console app. One with simple one-time task that end after doing it's job. You shouldn't stop it with readKey or something like that. Let it close. Other type is infinit app that run and do jubs until you close it explicitly. Like web apps. It use a infinit loop or something like that. In this kind, you normally close it with ctrl+c or a method that end all pending taks and exit safely
Am doing console apps
Обсуждают сегодня