Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this tutorial, you create a simple utility 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 10, it can be called by any application that targets .NET 10. This tutorial shows how to target .NET 10.
When you create a class library, you can distribute it as a third-party component or as a bundled component with one or more applications.
Prerequisites
- The latest .NET SDK
- Visual Studio Code editor
- The C# DevKit
Installation instructions
On Windows, this WinGet configuration file to install all prerequisites. If you already have something installed, WinGet will skip that step.
- Download the file and double-click to run it.
- Read the license agreement, type y, and select Enter when prompted to accept.
- If you get a flashing User Account Control (UAC) prompt in your Taskbar, allow the installation to continue.
On other platforms, you need to install each of these components separately.
- Download the recommended installer from the .NET SDK download page and double-click to run it. The download page detects your platform and recommends the latest installer for your platform.
- Download the latest installer from the Visual Studio Code home page and double click to run it. That page also detects your platform and the link should be correct for your system.
- Click the "Install" button on the C# DevKit extension page. That opens Visual Studio code, and asks if you want to install or enable the extension. Select "install".
Create a class library project
Start by creating a .NET class library project named "StringLibrary" and an associated solution. A solution serves as a container for one or more projects. You'll add additional, related projects to the same solution.
Start Visual Studio Code.
Go to the Explorer view and select Create .NET Project. Alternatively, you can bring up the Command Palette using Ctrl+Shift+P (Command+Shift+P on MacOS) and then type ".NET" and find and select the .NET: New Project command.
Choose the project template Class Library.
Then select the location where you would like the new project to be created: Create a folder named
ClassLibraryProjectsand select it.Name the project StringLibrary.
Select .sln as the solution file format.
Select Show all template options.
Next select .NET 10. Then select Create Project.
In the Do you trust the authors of the files in this folder? dialog, select Yes, I trust the authors. You can trust the authors because this folder only has files generated by .NET and added or modified by you.
The project is created and Class1.cs opens.
Replace the contents of Class1.cs with the following code:
namespace UtilityLibraries; public static class StringLibrary { public static bool StartsWithUpper(this string? str) { if (string.IsNullOrWhiteSpace(str)) return false; return char.IsUpper(str[0]); } }The class library,
UtilityLibraries.StringLibrary, contains a method namedStartsWithUpper. This method returns a Boolean value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. The Char.IsUpper(Char) method returnstrueif a character is uppercase.StartsWithUpperis implemented as an extension method so that you can call it as if it were a member of the String class.Save the file.
Expand Solution Explorer at the bottom of the Explorer view.
Right click the solution in Solution Explorer and select Build, or open the Command Palette and select .NET: Build to build the solution and verify that the project compiles without error.
The terminal output looks like the following example:
Determining projects to restore... All projects are up-to-date for restore. StringLibrary -> C:\Projects\ClassLibraryProjects\StringLibrary\bin\Debug\net10.0\StringLibrary.dll Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:02.78
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.
Right-click the solution in Solution Explorer and select New Project, or in the Command Palette select .NET: New Project.
Select Console App.
Give it the name ShowCase, select the default directory and select Create Project.
Open ShowCase/Program.cs and replace all of 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 code uses the
rowvariable 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.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.
Save your changes.
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 Solution Explorer right click on the ShowCase project and select Add Project Reference.
Select StringLibrary.
Tip
Alternatively, add the following to ShowCase.csproj:
<ItemGroup>
<ProjectReference Include="..\StringLibrary\StringLibrary.csproj" />
</ItemGroup>
Run the app
Use the top menu bar to select Run > Run without debugging.
Select C#.
Select C#: ShowCase.
If you get an error that says no C# program is loaded, close the folder that you have open, and open the
ShowCasefolder. Then try running the app again.Try out the program by entering strings and pressing Enter, then press Enter to exit.
The terminal output looks like the following example:
Press <Enter> only to exit; otherwise, enter a string and press <Enter>: A string that starts with an uppercase letter Input: A string that starts with an uppercase letter Begins with uppercase? : Yes a string that starts with a lowercase letter Input: a string that starts with a lowercase letter Begins with uppercase? : No
Additional resources
Next steps
In this tutorial, you created 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.