Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio execution closes when pressing enter

Hello im trying to learn C# step by step. I installed Visual Studio to practice but 20 mins in I cant test my basic code when executing:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("What is your name?");
            string name = Console.ReadLine();
            Console.WriteLine("My name is " + name);
        }
    }
}

It is as basic as this but when I execute and type a name and press enter, the cmd just closes. any help would be appreciated because i am enthusiastic to start out with C#

cmd


1 Answers

The Main method returns after

Console.WriteLine("My name is " + name);

And this effectively terminates the app. You should put a

Console.Read();

to wait until the next keystroke.

like image 122
PepitoSh Avatar answered Feb 20 '26 02:02

PepitoSh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!