Solving type or namespace not found in .NET Core

August 26, 20191 Min Read
Error CS0246 - Type or namespace could not be found. What does it mean? Why does it happen? How can you solve it?

Something strange happened today.

I've developed a Class Library in .NET Framework and I tested it with the related test library.

So I've integrated the library into another project. Everything was fine, I used that library without problems. But, all of a sudden:

Error CS0246: The type or namespace name 'XX' could not be found (are you missing a using directive or an assembly reference?).

What?!? I've been using it for the whole week. I've tested it. I've added the reference, and the Intellisense works as well.

C'mon, the class is here!

Clean the solution... nothing happens.

Close Visual Studio... still nothing.

Have a coffee... well, better now.

Suddenly, a doubt: what if I've created it in .NET Core instead of .NET Framework?

And, obviously... no, it wasn't that.

But I was close: the Class Library was in .NET Framework 4.7.2 but the application was in .NET Framework 4.7.1. It was the patch version!

So, I just downgraded the class library version through Visual Studio, and everything went well again.

You can find the version in the Properties screen of the project (right-click on the project in the Solution Explorer or Alt+Enter), in the Application tab, then under the Target framework field.

.NET version selection on Visual Studio 2019

So, the lesson is: know your enemy, and check for the slight details.

Hey, what do you think of this article?
Let me know on Twitter!

Of course, if you have suggestions on some topics you'd like to read about, just ask!