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.
Asked
Active
Viewed 4,838 times
4

user246392
- 2,661
- 11
- 54
- 96
2 Answers
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
-
@Agredo This is for PCL though, right? In platform specific projects, I see multiple PropertyGroups. – user246392 Sep 02 '20 at 15:52
-
But are you supposed to to raise the language like that or will something in my solution break? – lolelo Jul 23 '21 at 14:16
-
Great answer clear and concise + 1 for mention james hes one my idols as well. – c-sharp-and-swiftui-devni Sep 19 '21 at 23:15
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.
You will refer to this official document to know its explains.
And also can set C#
version manually in .csproj
as follow:
<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Here is the C# language version reference.

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