Tutorial: Create a .NET console application using Visual Studio for Mac

This tutorial shows how to create and run a .NET console application using Visual Studio for Mac.

Important

Microsoft has announced the retirement of Visual Studio for Mac. Visual Studio for Mac will no longer be supported starting August 31, 2024. Alternatives include:

  • Visual Studio Code with the C# Dev Kit and related extensions, such as .NET MAUI and Unity.
  • Visual Studio running on Windows in a VM on Mac.
  • Visual Studio running on Windows in a VM in the Cloud.

For more information, see Visual Studio for Mac retirement announcement.

Prerequisites

Create the app

  1. Start Visual Studio for Mac.

  2. Select New in the start window.

    New button on the Visual Studio for Mac Start screen

  3. In the New Project dialog, select App under the Web and Console node. Select the Console Application template, and select Next.

    New project templates list

  4. In the Target Framework drop-down of the Configure your new Console Application dialog, select .NET 5.0, and select Next.

  5. Type "HelloWorld" for the Project Name, and select Create.

    Configure your new Console Application dialog

The template creates a simple "Hello World" application. It calls the Console.WriteLine(String) method to display "Hello World!" in the terminal window.

The template code defines a class, Program, with a single method, Main, that takes a String array as an argument:

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Main is the application entry point, the method that's called automatically by the runtime when it launches the application. Any command-line arguments supplied when the application is launched are available in the args array.

Run the app

  1. Press (option+command+enter) to run the app without debugging.

    The terminal shows Hello World!

  2. Close the Terminal window.

Enhance the app

Enhance the application to prompt the user for their name and display it along with the date and time.

  1. In Program.cs, replace the contents of the Main method, which is the line that calls Console.WriteLine, with the following code:

    Console.WriteLine("What is your name?");
    var name = Console.ReadLine();
    var currentDate = DateTime.Now;
    Console.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}!");
    Console.Write($"{Environment.NewLine}Press any key to exit...");
    Console.ReadKey(true);
    

    This code displays a prompt in the console window and waits until the user enters a string followed by the enter key. It stores this string in a variable named name. It also retrieves the value of the DateTime.Now property, which contains the current local time, and assigns it to a variable named currentDate. And it displays these values in the console window. Finally, it displays a prompt in the console window and calls the Console.ReadKey(Boolean) method to wait for user input.

    NewLine is a platform-independent and language-independent way to represent a line break. Alternatives are \n in C# and vbCrLf in Visual Basic.

    The dollar sign ($) in front of a string lets you put expressions such as variable names in curly braces in the string. The expression value is inserted into the string in place of the expression. This syntax is referred to as interpolated strings.

  2. Press (option+command+enter) to run the app.

  3. Respond to the prompt by entering a name and pressing enter.

    Terminal shows modified program output

  4. Close the terminal.

Next steps

In this tutorial, you created a .NET console application. In the next tutorial, you debug the app.