Building a class library with C# and .NET Core in Visual Studio 2017

A class library defines types and methods that are called by an application. A class library that targets the .NET Standard 2.0 allows your library to be called by any .NET implementation that supports that version of the .NET Standard. When you finish your class library, you can decide whether you want to distribute it as a third-party component or whether you want to include it as a bundled component with one or more applications.

Note

For a list of the .NET Standard versions and the platforms they support, see .NET Standard.

In this topic, you'll create a simple utility library that contains a single string-handling method. You'll implement it as an extension method so that you can call it as if it were a member of the String class.

Creating a class library solution

Start by creating a solution for your class library project and its related projects. A Visual Studio Solution just serves as a container for one or more projects. To create the solution:

  1. On the Visual Studio menu bar, choose File > New > Project.

  2. In the New Project dialog, expand the Other Project Types node, and select Visual Studio Solutions. Name the solution "ClassLibraryProjects" and select the OK button.

    New project dialog

Creating the class library project

Create your class library project:

  1. In Solution Explorer, right-click on the ClassLibraryProjects solution file and from the context menu, select Add > New Project.

  2. In the Add New Project dialog, expand the Visual C# node, then select the .NET Standard node followed by the Class Library (.NET Standard) project template. In the Name text box, enter "StringLibrary" as the name of the project. Select OK to create the class library project.

    Add New Project dialog

    The code window then opens in the Visual Studio development environment.

    Visual Studio application window showing the default class library template code

  3. Check to make sure that our library targets the correct version of the .NET Standard. Right-click on the library project in the Solution Explorer windows, then select Properties. The Target Framework text box shows that we're targeting .NET Standard 2.0.

    Project properties for the class library

  4. Replace the code in the code window with the following code and save the file:

    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);
            }
        }
    }
    

    The class library, UtilityLibraries.StringLibrary, contains a method named StartsWithUpper, which 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 System.Char.IsUpper(Char) method returns true if a character is uppercase.

  5. On the menu bar, select Build > Build Solution. The project should compile without error.

    Output pane showing that the build succeeded

Next step

You've successfully built the library. Because you haven't called any of its methods, you don't know whether it works as expected. The next step in developing your library is to test it by using a Unit Test Project.