Introduction: The Labyrinth of .NET Libraries
\n\nIf you've ever created a new project in Visual Studio or Rider, you've probably seen two similar but confusing templates: Class Library (.NET Core) and Class Library (.NET Standard). A logical question arises: "What's the actual difference? And what should I choose so I don't have to rewrite everything from scratch later?"
\n\nThis isn't just an academic interest. This question has gathered thousands of views on Stack Overflow, and confusion between these two project types is one of the most common mistakes beginners make. In this article, we'll settle this dilemma once and for all, look at real C# code examples, and figure out which library type is right for your task.
\n\n1. A Brief History Lesson: From .NET Framework to the Era of Unification
\n\nTo understand the difference, we need to recall how the platform evolved. Before, there was only .NET Framework — a monolithic environment for Windows. Then came .NET Core — a cross-platform, lightweight version. And here a problem arose: how do you write a library that would work on both? To solve this, .NET Standard was created — a formal specification of APIs that are guaranteed to exist in all .NET implementations.
\n\nHowever, with the release of .NET 5 (and later .NET 6, 7, 8), Microsoft unified the platforms. Now there is a single base class — .NET (formerly .NET Core). This significantly changed the rules of the game. Let's break down what this means for us, developers.
2. What is Class Library (.NET Standard)?
\n\n.NET Standard is not a runtime, but a set of contracts. Think of it as a "universal adapter." If you create a library targeting netstandard2.0, it will work on:
- \n
- .NET Framework 4.6.1+ \n
- .NET Core 2.0+ \n
- .NET 5/6/7/8 \n
- Xamarin, Mono, Unity, and even UWP \n
This is the ideal choice if you need maximum platform coverage. You write the code once — and it works everywhere that supports this version of the standard.
\n\nCode Example for a .NET Standard Library
\n\nLet's create a simple string manipulation library targeting netstandard2.0:
// StringHelper.cs\nusing System;\nusing System.Text.RegularExpressions;\n\nnamespace MyStandardLibrary\n{\n public class StringHelper\n {\n /// <summary>\n /// Removes all HTML tags from a string.\n /// </summary>\n public static string StripHtmlTags(string input)\n {\n if (string.IsNullOrEmpty(input))\n return string.Empty;\n\n // Simple but effective regex for removing tags\n return Regex.Replace(input, "<.*?>", string.Empty);\n }\n\n /// <summary>\n /// Reverses a string.\n /// </summary>\n public static string ReverseString(string input)\n {\n if (string.IsNullOrEmpty(input))\n return string.Empty;\n\n char[] chars = input.ToCharArray();\n Array.Reverse(chars);\n return new string(chars);\n }\n }\n}\n\nNotice: we are only using base classes (System, System.Text.RegularExpressions) that are guaranteed to exist in all .NET Standard 2.0 implementations. This library will even work in a .NET Framework 4.6.1 project, which Microsoft no longer develops but is still used in the enterprise sector.
3. What is Class Library (.NET Core / .NET)?
\n\nWhen you choose .NET Core Class Library (or simply .NET Class Library, if you have .NET 5+), you are tying yourself to a specific platform implementation. In the project file (csproj), you'll see something like <Target