Tutorial: Create a .NET class library 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.

In this tutorial, you create a class library that contains a single string-handling method.

A class library defines types and methods that are called by an application. If the library targets .NET Standard 2.0, it can be called by any .NET implementation (including .NET Framework) that supports .NET Standard 2.0. If the library targets .NET 5, it can be called by any application that targets .NET 5. This tutorial shows how to target .NET 5.

Note

Your feedback is highly valued. There are two ways you can provide feedback to the development team on Visual Studio for Mac:

  • In Visual Studio for Mac, select Help > Report a Problem from the menu or Report a Problem from the Welcome screen, which opens a window for filing a bug report. You can track your feedback in the Developer Community portal.
  • To make a suggestion, select Help > Provide a Suggestion from the menu or Provide a Suggestion from the Welcome screen, which takes you to the Visual Studio for Mac Developer Community webpage.

Prerequisites

Create a solution with a class library project

A Visual Studio solution serves as a container for one or more projects. Create a solution and a class library project in the solution. You'll add additional, related projects to the same solution later.

  1. Start Visual Studio for Mac.

  2. In the start window, select New Project.

  3. In the Choose a template for your new project dialog select Web and Console > Library > Class Library, and then select Next.

    New Project dialog

  4. In the Configure your new Class Library dialog, choose .NET 5.0, and select Next.

  5. Name the project "StringLibrary" and the solution "ClassLibraryProjects". Leave Create a project directory within the solution directory selected. Select Create.

    Visual Studio for Mac New project dialog options

  6. From the main menu, select View > Solution, and select the dock icon to keep the pad open.

    Dock icon for Solution pad

  7. In the Solution pad, expand the StringLibrary node to reveal the class file provided by the template, Class1.cs. ctrl-click the file, select Rename from the context menu, and rename the file to StringLibrary.cs. Open the file and replace the contents with the following code:

    using System;
    
    namespace UtilityLibraries
    {
        public static class StringLibrary
        {
            public static bool StartsWithUpper(this string str)
            {
                if (string.IsNullOrWhiteSpace(str))
                    return false;
    
                char ch = str[0];
                return char.IsUpper(ch);
            }
        }
    }
    
  8. Press S (command+S) to save the file.

  9. Select Errors in the margin at the bottom of the IDE window to open the Errors panel. Select the Build Output button.

    Bottom margin of the Visual Studio Mac IDE showing the Errors button

  10. Select Build > Build All from the menu.

    The solution builds. The build output panel shows that the build is successful.

    Visual Studio Mac Build output pane of the Errors panel with Build successful message

Add a console app to the solution

Add a console application that uses the class library. The app will prompt the user to enter a string and report whether the string begins with an uppercase character.

  1. In the Solution pad, ctrl-click the ClassLibraryProjects solution. Add a new Console Application project by selecting the template from the Web and Console > App templates, and select Next.

  2. Select .NET 5.0 as the Target Framework and select Next.

  3. Name the project ShowCase. Select Create to create the project in the solution.

    Add ShowCase project

  4. Open the Program.cs file. Replace the code with the following code:

    using System;
    using UtilityLibraries;
    
    class Program
    {
        static void Main(string[] args)
        {
            int row = 0;
    
            do
            {
                if (row == 0 || row >= 25)
                    ResetConsole();
    
                string? input = Console.ReadLine();
                if (string.IsNullOrEmpty(input)) break;
                Console.WriteLine($"Input: {input} {"Begins with uppercase? ",30}: " +
                                  $"{(input.StartsWithUpper() ? "Yes" : "No")}{Environment.NewLine}");
                row += 3;
            } while (true);
            return;
    
            // Declare a ResetConsole local method
            void ResetConsole()
            {
                if (row > 0)
                {
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
                Console.Clear();
                Console.WriteLine($"{Environment.NewLine}Press <Enter> only to exit; otherwise, enter a string and press <Enter>:{Environment.NewLine}");
                row = 3;
            }
        }
    }
    

    The program prompts the user to enter a string. It indicates whether the string starts with an uppercase character. If the user presses the enter key without entering a string, the application ends, and the console window closes.

    The code uses the row variable to maintain a count of the number of rows of data written to the console window. Whenever it's greater than or equal to 25, the code clears the console window and displays a message to the user.

Add a project reference

Initially, the new console app project doesn't have access to the class library. To allow it to call methods in the class library, create a project reference to the class library project.

  1. In the Solutions pad, ctrl-click the Dependencies node of the new ShowCase project. In the context menu, select Add Reference.

  2. In the References dialog, select StringLibrary and select OK.

Run the app

  1. ctrl-click the ShowCase project and select Run project from the context menu.

  2. Try out the program by entering strings and pressing enter, then press enter to exit.

    Visual Studio for Mac console window showing your app running

Additional resources

Next steps

In this tutorial, you created a solution and a library project, and added a console app project that uses the library. In the next tutorial, you add a unit test project to the solution.