Tutorial: Create a .NET class library using Visual Studio Code

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 8, it can be called by any application that targets .NET 8. This tutorial shows how to target .NET 8.

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

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.

  1. Start Visual Studio Code.

  2. 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.

  3. After selecting the command, you'll need to choose the project template. Choose Class Library.

  4. Then select the location where you would like the new project to be created.

  5. Then select the location where you would like the new project to be created: Create a folder named ClassLibraryProjects and select it.

  6. Name the project StringLibrary, select Show all template options, select .NET 8 and select Create Project.

  7. Name the project StringLibrary and select Create Project.

  8. Press Enter at the prompt Project will be created in <path>.

  9. Check to make sure that the library targets .NET 8. In Explorer, open StringLibrary/StringLibrary.csproj.

    The TargetFramework element shows that the project targets .NET 8.0.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
      </PropertyGroup>
    
    </Project>
    
  10. Open Class1.cs and replace the code with the following code.

    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. 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 returns true if a character is uppercase.

    StartsWithUpper is implemented as an extension method so that you can call it as if it were a member of the String class.

  11. Save the file.

  12. Expand Solution Explorer at the bottom of the Explorer view.

  13. 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:

    Microsoft (R) Build Engine version 17.8.0+b89cb5fde for .NET
    Copyright (C) Microsoft Corporation. All rights reserved.
      Determining projects to restore...
      All projects are up-to-date for restore.
      StringLibrary -> C:\Projects\ClassLibraryProjects\StringLibrary\bin\Debug\net8.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.

  1. Right-click the solution in Solution Explorer and select New Project, or in the Command Palette select .NET: New Project.

  2. Select Console app.

  3. Give it the name ShowCase, select the default location and select Create Project.

  4. Open ShowCase/Program.cs and replace all of the code with the following code.

    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}");
                Console.WriteLine("Begins with uppercase? " +
                     $"{(input.StartsWithUpper() ? "Yes" : "No")}");
                Console.WriteLine();
                row += 4;
            } 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 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.

    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.

  5. 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.

  1. In Solution Explorer right click on the ShowCase project and select Add Project Reference.

  2. Select StringLibrary.

Run the app

  1. Select Run > Run without debugging.

  2. Select C#.

  3. Select ShowCase.

    If you get an error that says no C# program is loaded, close the folder that you have open, and open the ShowCase folder. Then try running the app again.

  4. 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.

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 7, it can be called by any application that targets .NET 7. This tutorial shows how to target .NET 7.

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

Create a class library project and solution

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.

  1. Start Visual Studio Code.

  2. 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.

  3. After selecting the command, you'll need to choose the project template. Choose Class Library.

  4. Then select the location where you would like the new project to be created.

  5. Give it the name StringLibrary, select Show all template options, select .NET 7 and select Create Project.

  6. Check to make sure that the library targets .NET 7. In Explorer, open StringLibrary/StringLibrary.csproj.

    The TargetFramework element shows that the project targets .NET 7.0.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
      </PropertyGroup>
    
    </Project>
    
  7. Open Class1.cs and replace the code with the following code.

    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. 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 returns true if a character is uppercase.

    StartsWithUpper is implemented as an extension method so that you can call it as if it were a member of the String class.

  8. Save the file.

  9. Right click on the project in Solution Explorer and right-click 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:

    Microsoft (R) Build Engine version 16.7.4+b89cb5fde for .NET
    Copyright (C) Microsoft Corporation. All rights reserved.
      Determining projects to restore...
      All projects are up-to-date for restore.
      StringLibrary -> C:\Projects\ClassLibraryProjects\StringLibrary\bin\Debug\net7.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.

  1. In the Solution Explorer select New Project or in the Command Palette select .NET: New Project.

  2. Select Console app.

  3. Give it the name ShowCase, select the default location and select Create Project.

  4. Open ShowCase/Program.cs and replace all of the code with the following code.

    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}");
                Console.WriteLine("Begins with uppercase? " +
                     $"{(input.StartsWithUpper() ? "Yes" : "No")}");
                Console.WriteLine();
                row += 4;
            } 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 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.

    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.

  5. 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.

  1. In Solution Explorer right click on the ShowCase project and select Add Project Reference.

  2. Select StringLibrary.

Run the app

  1. Select Run>Run without debugging.

  2. Select C#.

  3. Select ShowCase.

  4. 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 solution, added 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.

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 6, it can be called by any application that targets .NET 6. This tutorial shows how to target .NET 6.

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

Create a class library project and solution

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.

  1. Start Visual Studio Code.

  2. 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.

  3. After selecting the command, you'll need to choose the project template. Choose Class Library.

  4. Then select the location where you would like the new project to be created.

  5. Give it the name StringLibrary, select Show all template options, select .NET 7 and select Create Project.

  6. Check to make sure that the library targets .NET 6. In Explorer, open StringLibrary/StringLibrary.csproj.

    The TargetFramework element shows that the project targets .NET 6.0.

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
      </PropertyGroup>
    
    </Project>
    
  7. Open Class1.cs and replace the code with the following code.

    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. 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 returns true if a character is uppercase.

    StartsWithUpper is implemented as an extension method so that you can call it as if it were a member of the String class.

  8. Save the file.

  9. Right click on the project in Solution Explorer and right-click 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:

    Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET
    Copyright (C) Microsoft Corporation. All rights reserved.
      Determining projects to restore...
      All projects are up-to-date for restore.
      StringLibrary -> C:\Projects\ClassLibraryProjects\StringLibrary\bin\Debug\net6.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.

  1. In the Solution Explorer select New Project or in the Command Palette select .NET: New Project.

  2. Select Console app.

  3. Give it the name ShowCase, select the default location and select Create Project.

  4. Open ShowCase/Program.cs and replace all of the code with the following code.

    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}");
                Console.WriteLine("Begins with uppercase? " +
                     $"{(input.StartsWithUpper() ? "Yes" : "No")}");
                Console.WriteLine();
                row += 4;
            } 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 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.

    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.

  5. 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.

  1. In Solution Explorer right click on the ShowCase project and select Add Project Reference.

  2. Select StringLibrary.

Run the app

  1. Select Run>Run without debugging.

  2. Select C#.

  3. Select ShowCase.

  4. 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 solution, added 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.