0

I want to create a library to use in .Net framework applications and Xamarin applications. But there are 3 framework versions:

  • .net core
  • .net framework
  • .net standart

So I could not decide which version to use my common library.

  1. If I create a .net standart library, does it work in .net framework and .net core.
  2. If I create a .net core library, does it work in .net framework and .net standart.

I am confused about frameworks.

barteloma
  • 6,403
  • 14
  • 79
  • 173
  • 2
    Maybe [this](https://stackoverflow.com/questions/42939454/what-is-the-difference-between-net-core-and-net-standard-class-library-project) will help – Izzy Apr 29 '19 at 06:43

3 Answers3

3

This might help you decide

  1. .Net Standard : Used for building libraries that can be referenced from all .NET implementations, such as .NET Framework, .NET Core and Xamarin
  2. .Net Core : Used for building cross-platform console apps and ASP.NET Core Web apps and cloud services.

So when if you want your library to be supported by all different type of .Net implementations, .Net standard is the way to go.

Muhammad Hannan
  • 2,389
  • 19
  • 28
  • Does the version numbers matter? For example: Does .net standart x vesion supports .net framework y version? – barteloma Apr 29 '19 at 08:30
  • .Net standard have different set of APIs available for each version like .Net standard 2.0 have more APIs than previous versions. Each version is fully supported in .Net framework versions. – Muhammad Hannan Apr 29 '19 at 08:47
  • @MuhammadHannan: _Each version is fully supported in .Net framework versions_ Aaaaaaaahh, should be careful with that statement. This is so far only true for the currently released .NET Standard versions (1.0-1.6, 2.0). .NET Standard 2.1 will most-likely (no final version released yet, so the decision may change) add API which won't be compatible with any (and probably future) versions of .NET Framework. See [Announcing .NET Standard 2.1](https://devblogs.microsoft.com/dotnet/announcing-net-standard-2-1/). – Tseng Apr 29 '19 at 09:34
  • Reason for that is explained in the blog (basically the new `Span` and more apis come to .NET standard 2.1) and .NET Framework won't implement these as the changes require significant amount of work on the .NET Framework runtime to add support for these due to the high risk of breaking backward compatibility (since .NET framework runs on over a billion machines and doesn't support side-by-side runtimes like .NET Core does) – Tseng Apr 29 '19 at 09:36
  • @Tseng Thanks for mentioning that. That's why I have only mentioned v2.0. – Muhammad Hannan Apr 29 '19 at 09:40
  • Well the "**Each version**" (rather than "Up to Version 2.0") reads differently, that's why I added the comment – Tseng Apr 29 '19 at 09:42
2

As you're writing a C# library I imagine you know the difference between a class and an interface. A class is a concrete implementation of some feature set, and an interface defines what features you can expect from an instance that implements it.

Using this as an example, .NET Framework and .NET Core are like classes. .NET Framework is the "classic" implementation, and .NET Core is a newer implementation which has advantages such as being able to run on Linux. If you build a library to target .NET Core or .NET Framework, you are building it to target one of those concrete implementations.

.NET Standard on the other hand is like an interface. Each version of .NET Standard provides a set of features, and different versions of .NET Framework/.NET Core implement different versions of .NET Standard. When you build a library that targets a given version of .NET Standard, you're saying that you can support all of the concrete implementations in the corresponding column of that table.

Deciding what version of .NET Standard to target will depend on what functionality you need to implement your library. More features usually means a higher version and supporting fewer implementations. Fewer features means a lower version and more widespread support.

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
1

So many confusing other answers here.

First and foremost it depends on what platforms you are targeting.

  1. is it a generic use library you want share with the world?
  2. is it an application specific library (i.e. reusable code for the same application, i.e. a line of business application) which is to be used on multiple platforms

Case 1

You should go with .NET Standard, since it gives you the most platforms. Which version you use, depends on the features you need.

If you want reach the most platforms, try to target as low as possible (.NET Standard 1.0, it targets .NET Core 1.0, .NET Framework 4.5, Mono, Xamarin iOS 10/Mac 3.0/Android 7.0, UWP 10 and Uniy 2018.1 and all newer versions of these).

You can see the exact .NET Standard Matrix in the provided link.

If you need specific API you have to target a higher version, such as .NET Standard 2.0 which a lot (~22k new APIs from .NET Framework have been ported to .NET Core 2.0 from 1.1) APIs than .NET Standard 1.1.

This may not allow you all APIs (no WPF/WinForm specific apis) but in generic use reusable libraries this shouldn't be an issue.

Case 2

Here, you can also apply Case 1 tips, if possible.

If that doesn't cover your required Apis and you know you don't want target .NET Core or Unity, you can still use the old styled PCLs: Portable Class Library.

They are a more complicated versions of .NET Standard (kinda a predecessor of .NET Standard), where depending on which platform you target the API surface shrinks to only allow APIs to be used which run on all these platforms.

It's not recommended these days to use PCLs, since .NET Standard is preferred and more easy (for library authors) to use and target multiple platforms.

Last but not least, if you really need some feature only on Windows and .NET Framework (or you don't care for .NET Core), you can still cross-target, i.e. have a .NET Standard 2.0 for all platforms and add specific APIs only to net45 target and preprocessor instructions (#if NET45/#endif).

This compiles into two libraries, one for netstandard2.0 and one for net45 (.NET Framework 4.5).

Community
  • 1
  • 1
Tseng
  • 61,549
  • 15
  • 193
  • 205