16

In the .csproj file in my .NET Core projects, there are these 3 lines by default:

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

Now if I want to target mulitple frameworks, I can change it to this:

<PropertyGroup>
  <TargetFrameworks>netcoreapp2.2;net4.6</TargetFrameworks>
</PropertyGroup>

The difference is subtle, but it's there. When targeting multiple frameworks, you have to use <TargetFrameworks> (plural) instead of just <TargetFramework> (singular).

But why is it made like this? It seems like it would have been easier to just pick one of the two, and then always use that. Which leads me to the thought, that there might be a more complex reason, for choosing to different (although similar) words, depending on whether or not you target more frameworks. Can anyone enlighten me on the topic?

Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96
  • Just something they did to help programmers with existing projects (about everybody) migrate to .NETCore. They'll keep deploying to .NETFramework for a while, but can be sure there will be few surprises when they finally adopt .NETCore. This will disappear, eventually. – Hans Passant Dec 05 '19 at 11:28
  • note that there are other properties like this as well. Platform(s) and I think also Configuration(s), at least. they might be a little more flexible though (can use 2 with the singular or 1 with the plural; not 100% sure). – Dave Cousineau Dec 12 '20 at 19:07

2 Answers2

7

Basically when you want to target a single framework you use <TargetFramework> tag (in the case where you're building an app targeting .net-core), but it's possible also that you may conditionally reference assemblies multiple frameworks by using <TargetFrameworks> (in case you're building an app for both .net standard and .net-core) and then you can conditionally compile against those assemblies by using preprocessor symbols with if-then-else logic

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard1.4;net40;net45</TargetFrameworks>
  </PropertyGroup>

  <!-- Conditionally obtain references for the .NET Framework 4.0 target -->
  <ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
    <Reference Include="System.Net" />
  </ItemGroup>

  <!-- Conditionally obtain references for the .NET Framework 4.5 target -->
  <ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Threading.Tasks" />
  </ItemGroup>

</Project>

Source : https://learn.microsoft.com/fr-fr/dotnet/standard/frameworks

Tim
  • 5,435
  • 7
  • 42
  • 62
Fourat
  • 2,366
  • 4
  • 38
  • 53
  • I appreciate your input, but why does it have to be two different tags (`` and ``), rather than just one of them? – Jakob Busk Sørensen Dec 05 '19 at 11:06
  • @Noceo I agree it would be simpler to have only one tag but I guess it was the developers choice, you can ask them by opening an issue here : https://github.com/dotnet/core/issues – Fourat Dec 05 '19 at 11:55
  • 2
    I might do that. It would be interesting to hear the thought behind the design :-) – Jakob Busk Sørensen Dec 05 '19 at 12:46
  • How is it possible to be using the non-plural TargetFramework property in the ItemGroup conditions when you only set the plural TargetFrameworks property? – Kelsie Sep 29 '20 at 13:52
-1

You can use TargetFrameworks configuration when you are building a class library that will runs well in .Net Full Framework and .Net Core Framework. This is the best use case to this configuration

It doesn't make sense use multiple target framework if you are building a website or webapi. In this case, use just one target framework.

Anderson
  • 89
  • 3
  • 1
    Thanks for your input. My question was more targeted towards the decission of making two different wordings for the same features (as I see it). I don't get that implementation. I do get (at least I think I do) how to use it... – Jakob Busk Sørensen Dec 05 '19 at 11:04
  • Humm, but it's not the same feature.. it's similar. If you want to target just one framework (aka .net core and it doesn't run on .net classic), then put them into TargetFramework. If you want to run your app both in .net framework classic and in .net core, you need to put this setting in TargetFrameworks. I agree with you, it's a bit strange, it would be one configuration: targetFramework. If I want just one, put one. If I want more than one, put all that we want. But, it is as-is. – Anderson Dec 05 '19 at 11:37