How to use the latest C# version in Xamarin Forms projects

Viewed 3591

I have a Xamarin Forms application. I'd like to use the latest C# version in platform specific projects (i.e. Xamarin.iOS and Xamarin.Android). It looks like I can add <LangVersion>latest</LangVersion> to the .csproj files. However, I'm not sure where to add it exactly. I see a lot of PropertyGroup tags in the project files (usually one for each simulator and release type). Do I need to add it to every PropertyGroup? I need the latest language version to be available when debugging and in production.

2 Answers

James Montamagno posted a tutorial on his Blog here: https://montemagno.com/use-csharp-8-everywehre/

Here you see the XML Code:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <ProduceReferenceAssembly>true</ProduceReferenceAssembly>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Xamarin.Forms" Version="4.3.0.947036" />
    <PackageReference Include="Xamarin.Essentials" Version="1.3.1" />
  </ItemGroup>
</Project>

Generally, if you use the latest version of Target framework, the version of C# will be the latest. Then you will not need to set the version of C# manually.

Right click the solution of Xamarin Forms, then click Properties, then can set the Target framework as follow.

enter image description here

You will refer to this official document to know its explains.

enter image description here

And also can set C# version manually in .csproj as follow:

<PropertyGroup>
   <LangVersion>latest</LangVersion>
</PropertyGroup>

Here is the C# language version reference.

enter image description here

Related