C# 8 was officially released in September at .NET Conf along side .NET Core 3 & Visual Studio 16.3 (8.3 on Mac). It is packed with tons of amazing new features that truly everyone should be using. However, there is a lot of confusing as to if you can actually use C# 8 if you aren't using .NET Core 3 or .NET Standard 2.1.
- Short answer: YES! You can use it today.
- Long answer: YES! You just have to turn it on and perhaps add a NuGet package or two for certain features.
When you create a new app or library there is a "default" version of C# that is used. .NET Core 3 and .NET Standard 2.1 both use 8.0, but everything else basically defaults to 7.3. If you are like me, this is an issue because I use .NET Standard 2.0, which defaults to 7.3 :(. Don't worry though because if you are building on Visual Studio 2019 16.3 (or 8.3 on Mac) it includes the C# 8 compiler, which means you can use C# 8. If it compiles it should just work.
So, just go into your csproj where you want to use C# 8 and change the LangVersion to 8.0:
Here is what my .NET Standard Library looks like for my Xamarin.Forms apps:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <ProduceReferenceAssembly>true</ProduceReferenceAssembly> <LangVersion>8.0</LangVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Xamarin.Forms" Version="22.214.171.1247036" /> <PackageReference Include="Xamarin.Essentials" Version="1.3.1" /> </ItemGroup> </Project>
All Types Not Included
Nearly all of the C# 8 features are just syntax that can be compiled, but there are a few new types such as IAsyncEnumerable. Don't worry though as you can just install a NuGet package to get this new type similar to how we brought in ValueTuple in the past.
A Few Limitations
There are actually a few limitation to using C# 8 in non-.NET Core 3/.NET Standard 2.1 projects. A few of the features such and indexes & ranges are runtime features, which means they will not be available. That is alright though because if you try to use them, they wont compile! If you need them then you will actually need to upgrade, however there are very few limitations.
Xamarin & C# 8
One last thing that I should mention is that Xamarin apps already support all C# 8 features as of Visual Studio 16.3. This means if you turn on C# 8 in a Xamarin iOS or Android projects all of the features should be available to you. In a future update the default will be updated to 8.0 so you won't have to worry about this.
If you need more reasons to upgrade to C# 8, then listen to last week's Merge Conflict. Frank and I talked about all of our favorite features of C# 8 that you need right now.