Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 351
user246392 Avatar asked Oct 21 '25 07:10

user246392


2 Answers

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.

like image 72
Agredo Avatar answered Oct 23 '25 21:10

Agredo


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

like image 20
Junior Jiang Avatar answered Oct 23 '25 21:10

Junior Jiang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!