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
Install Visual Studio for Mac version 8.8 or later. Select the option to install .NET Core. Installing Xamarin is optional for .NET development. For more information, see the following resources:
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.
Start Visual Studio for Mac.
In the start window, select New Project.
In the Choose a template for your new project dialog select Web and Console > Library > Class Library, and then select Next.
In the Configure your new Class Library dialog, choose .NET 5.0, and select Next.
Name the project "StringLibrary" and the solution "ClassLibraryProjects". Leave Create a project directory within the solution directory selected. Select Create.
From the main menu, select View > Solution, and select the dock icon to keep the pad open.
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); } } }
Press ⌘S (command+S) to save the file.
Select Errors in the margin at the bottom of the IDE window to open the Errors panel. Select the Build Output button.
Select Build > Build All from the menu.
The solution builds. The build output panel shows that the build is successful.
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.
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.Select .NET 5.0 as the Target Framework and select Next.
Name the project ShowCase. Select Create to create the project in the solution.
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.
In the Solutions pad, ctrl-click the Dependencies node of the new ShowCase project. In the context menu, select Add Reference.
In the References dialog, select StringLibrary and select OK.
Run the app
ctrl-click the ShowCase project and select Run project from the context menu.
Try out the program by entering strings and pressing enter, then press enter to exit.
Additional resources
- Develop libraries with the .NET CLI
- Visual Studio 2019 for Mac Release Notes
- .NET Standard versions and the platforms they support.
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.