Which version of .NET am I using?
How do I figure out if I’m using the right version of .NET in my code?
When you create a new C# project, you have the option to pick a version of .NET.
But sometimes, you go back to look at a project and you no longer remember which version you chose. The version of .NET that your project uses will determine which C# language features you can use, as well as which version of the .NET runtime you need when you run your program.
In this post, I’ll show you two easy ways to look up the current .NET version of any of your projects.
Using Visual Studio and a graphical editor
If you have Visual Studio (there is usually very similar steps in any other IDE, like Visual Studio Code or Rider), then an easy way to look this up is to find your project in the Solution Explorer, right-click on it, and choose Properties:
Doing this will open a new tab where you can edit a wide variety of project properties in a graphical editor:
Make sure you’ve got Application > General selected, and then look for the dropdown labeled Target framework. This tells you which version of .NET this project is using.
From the screenshot above, you can see that this project is using .NET 6.0.
This is also a convenient place to change the version of .NET, if it isn’t what you were expecting. Open the dropdown and pick the version you want.
In older versions of Visual Studio, you might need to install the intended version of .NET before you can pick it from the dropdown.
The nice part about this approach is that a graphical editor like this will ensure that when you make changes, they’re done correctly, and you don’t accidentally introduce a typo or bug.
Editing the .csproj file
Every C# project has a file with the extension .csproj. This file is the project configuration.
The .NET version being used by a project appears inside of this file, and can be edited here as well.
You can open this file in any text editor to both find the current .NET version and change it to something else.
My HelloWorld2.csproj file looks like this in a text editor:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
A .csproj file is an XML file, so it can be viewed and edited in virtually any text editor.
That line that says <TargetFramework>net6.0</TargetFramework>
is the important one here.
Because the value inside of the TargetFramework
tags is net6.0
, we know this project is using .NET 6.
You can change that to be anything else, but the catch is in knowing what the allowed options are.
The content of that TargetFramework
element must be one of the allowed target framework monikers, or TFMs, as Microsoft calls them.
You can find the full list of allowed target framework monikers here: https://docs.microsoft.com/en-us/dotnet/standard/frameworks.
Most of my blog posts are answers to questions that readers of my book, The C# Player’s Guide have asked. If you’re interested in learning C# in depth, check it out: csharpplayersguide.com