4

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.

user246392
  • 2,661
  • 11
  • 54
  • 96

2 Answers2

6

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

You just have to change the <LangVersion /> value of your .csproj:

<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>

if you want to change the language of your platform specific code just add:

  <PropertyGroup>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>

to your ProjectName.iOS.csproj or ProjectName.Android.csproj.

Bryce Friha
  • 186
  • 2
  • 11
Agredo
  • 131
  • 1
  • 10
5

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

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • In the iOS and Android csproj files, there are multiple `PropertyGroup` tags. Do I need to add this for every single one of them (i.e. for every simulator and debug/release config)? – user246392 Sep 03 '20 at 14:40
  • 1
    @user246392 Not need. Only add this in Froms `.csproj` is ok. – Junior Jiang Sep 04 '20 at 02:01