🗯️ The Hidden Magic of .NET SDK-Style Project Files: What Changed Since .NET Framework 4.8
👋 Introduction
Welcome to my trouble with .NET SDK. I am working with .NET 6.0 and above versions of projects and I am familiar with it.
But, ever opened an older .NET framework project .csproj
file. When writing a project with .NET framework 4.8 first time, the coding feels normal with tiny deviation.
Project is build and ran correctly. Usually, I committed the relevant files into Github. The next day, it made me to feel what the hell is going on here.
🕵️ The Problem
The next day, when I was opened the project and try to build an application, it was failed. Visual studio is displaying that
Error : CS0246 : The type or namespace name 'Customer' could not be found (are you missing a using directive or an assembly reference?)
🧩 The Mystery
I was checked the followings:
- ✅ Is declared file name and namespace is correct → Yes
- ✅ Is it physically present under project directory → Yes
🔍 The Root Cause
Verified my project directory, all files are presenting correctly. I don’t have any other clue. Then I searched on the web regarding this.
Finally found that .NET framework project will use the files only which are included in .csproj
If any other files are presents under the directory, the project don’t even consider that 💥.
Usually some cases, when building the project, the files will be included in .csproj
file automatically.
But, I did not saved and committed the .csproj
file. That’s why, this is not displayed in the .csproj
file and Visual Studio
💡 The Lesson
Then why I did not add anything like this in .NET 6.0 and above versions of projects .csproj
file.
But, if any package references, some others, we have to add the entry. That’s clear.
Let’s come to the place to see, what is the hidden magic of .NET SDK-Style project files and what changed since .NET framework 4.8
Traditional .csproj
<Project>
<PropertyGroup>
<OutputType>Exe</OutputType>
.......
<TargetFrameworkVersion>vv4.8</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile> Include="Models\Manager.cs" />
<Compile> Include="Models\Customer.cs" />
<Compile> Include="Models\Invoice.cs" />
<Compile> Include="Models\School.cs" />
</ItemGroup>
</Project>
This is the reason behind the issue, where the file is physically available but did not presents in Visual Studio. My point is that including all files is the terrible thing and hard to grasp project structure.
Latest .csproj
Microsoft launched next version of .NET releases (.NET Core, .NET 5+, .NET 6+) with too many features, including the resolution for my trouble. That is called Implicit Includes.
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<Project>
This looks simple and easier to manage. This single line will make project to consider all files which are under project directory. That’s awesome 🎇.
But do not think that there will not be any work on .csproj
. Definitely NO 😃. There will be a chance to edit .csproj
file in such cases.
For example, if you want to exclude some files and copy some files to the output directory, definitely you want to edit the .csproj
file.
👋 Final Thoughts
The missing entries in modern .NET project files are not a bug, they are a powerful feature designed to simplify development and reduce boilerplate.
If you encounter issues like my .NET framework 4.8 experience, remember the explicit nature of .csproj
file.
Happy Coding!! ☕.