How to create shared library in .NET core?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AjayGohil
    New Member
    • Apr 2019
    • 83

    How to create shared library in .NET core?

    How to create shared library in .NET core?
  • natashasturrock
    New Member
    • Jul 2025
    • 14

    #2
    Creating a shared library in .NET Core (now just .NET, from .NET 5 onward) is straightforward using a Class Library project. Here's how to do it:

    Steps to Create a Shared Library in .NET Core
    Create the class library project:

    [HTML]bash
    dotnet new classlib -n MySharedLibrary
    This creates a .csproj and a basic Class1.cs.[/HTML]

    Write your shared code:
    Replace or add classes, interfaces, helpers, etc., inside the library project. Example:

    [HTML]csharp
    namespace MySharedLibrary
    {
    public class MathUtils
    {
    public static int Add(int a, int b) => a + b;
    }
    }[/HTML]

    Reference the shared library from another project:
    If you're using another .NET Core app or API, go to its directory and add a reference:

    [HTML]bash
    dotnet add reference ../MySharedLibrary/MySharedLibrary .csproj[/HTML]

    Use it in your main project:

    [HTML]csharp
    using MySharedLibrary ;

    var result = MathUtils.Add(2 , 3);[/HTML]
    (Optional) Package as a NuGet package:
    If you want to reuse it across solutions:

    [HTML]bash
    dotnet pack -c Release[/HTML]
    Then publish to a local or public NuGet feed.

    Structure Example

    [HTML]markdown
    /MySolution
    /MySharedLibrary
    MySharedLibrary .csproj
    /MyApp
    MyApp.csproj zz0.ovte40c14fz z[/HTML]

    Comment

    • natashasturrock
      New Member
      • Jul 2025
      • 14

      #3
      To create a shared library in .NET Core, you simply create a Class Library project. This library can contain reusable code like helper methods, services, or business logic. Once built, you can reference it from other .NET Core projects within the same solution or even package it as a NuGet package for wider reuse. It's a great way to keep your code modular and maintainable.

      Comment

      Working...