I have recently inherited a Visual Studio 2022 project, it is a C# Console application on .Net Framework 4.8. So I thought not a very old project what can go wrong.
Well, when I worked on the project, only into first few hours, I have faced following error:
Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
I got this error because I tried to use Nullable type in my new code I was developing. But I was thinking, why would this project is using C# 7.3? At the time of writing latest version of the C# was version 13, which was released with .NET 9.0 in 2024. So how come C# version used in relatively new project has taken version 7.3?
To find the answer I have turned to ChatGPT and web. This is what it had to tell:
The default version of the C# depends on the the target framework of your project. Here is the default versions for .NET Framework 4.6.2 and 4.8:
|
|
|
|
|
|
|
|
|
That explained lot. This project was created on .Net Framework 4.8, so it has defaulted to C# version 7.3.
Next question pop-up to me was, then how can I upgrade C# version on this project to more latest version, so I can use new features in the language in my code?
Answer is, you need to manually edit the .csproj file (i.e. Visual Studio project file). You need to insert something similar to below:
- .NET 8 => default to C# 12
- .NET 7 => default to C# 11
- .NET 6 => default to C# 10