Skip to main content

Fixed: "error CS0246: The type or namespace name 'Dewiride' could not be found (are you missing a using directive or an assembly reference?)"

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

error CS0246: The type or namespace name 'Dewiride' could not be found (are you missing a using directive or an assembly reference?) The compiler ends the message with a question, and it's a good one: those are the two usual causes. The name in quotes and the line number tell you which one you have. Here are both, from the same .NET 10 project, and the fix for each.

What I Was Doing​

A .NET 10 web API keeps its database settings class, DatabaseOptions, in a class library, and uses it in Program.cs:

src/Dewiride.Inventory.Api/Program.cs (excerpt)
using Dewiride.Inventory.Data;

var builder = WebApplication.CreateBuilder(args);

// DatabaseOptions lives in the class library.
builder.Services.Configure<DatabaseOptions>(builder.Configuration.GetSection("Database"));

That built fine. Then I removed the API's project reference to the library and built again:

Terminal
dotnet reference remove src/Dewiride.Inventory.Data/Dewiride.Inventory.Data.csproj --project src/Dewiride.Inventory.Api/Dewiride.Inventory.Api.csproj
dotnet build

What I Expected​

The build to succeed, as it had a moment earlier.

What I Got​

Output
Dewiride.Inventory.Data net10.0 succeeded (0.0s) → src/Dewiride.Inventory.Data/bin/Debug/net10.0/Dewiride.Inventory.Data.dll
Dewiride.Inventory.Api net10.0 failed with 1 error(s) (0.4s)
/Users/you/projects/dewiride-inventory-api/src/Dewiride.Inventory.Api/Program.cs(1,7): error CS0246: The type or namespace name 'Dewiride' could not be found (are you missing a using directive or an assembly reference?)

Build failed with 1 error(s) in 2.4s

A terminal where dotnet reference remove removes the API's reference to the class library, and dotnet build then fails with error CS0246: The type or namespace name 'Dewiride' could not be found, at Program.cs line 1, column 7.

What I Checked​

Where the error points. (1,7) is line 1, column 7: the word Dewiride in using Dewiride.Inventory.Data;. The compiler didn't fail on DatabaseOptions further down; it failed earlier, on the namespace itself. It couldn't find anything called Dewiride at all.

What the documentation lists. Microsoft's page on assembly reference errors groups CS0246 with the other "missing references" errors and lists the causes, starting with these two: "The project doesn't reference the required assembly." and "You misspelled the name of a type. Check the name of the type."

Why It Happens​

The namespace Dewiride.Inventory.Data is defined in the class library. Without the project reference, nothing from the library is part of the API's compilation, so the compiler can't find the namespace named in the using line, and it reports CS0246 on that namespace's first part, Dewiride. A missing reference.

The Fix​

Add the reference back:

Terminal
dotnet reference add src/Dewiride.Inventory.Data/Dewiride.Inventory.Data.csproj --project src/Dewiride.Inventory.Api/Dewiride.Inventory.Api.csproj
dotnet build

How to Add a Project Reference in .NET C# with the CLI explains the command and the <ProjectReference> it writes.

If the missing type comes from a NuGet package rather than your own project, the fix is the same idea: add the package to the project that uses it, with dotnet package add.

After the Fix​

Output
Reference `..\Dewiride.Inventory.Data\Dewiride.Inventory.Data.csproj` added to the project.

Dewiride.Inventory.Data net10.0 succeeded (0.0s) → src/Dewiride.Inventory.Data/bin/Debug/net10.0/Dewiride.Inventory.Data.dll
Dewiride.Inventory.Api net10.0 succeeded (0.6s) → src/Dewiride.Inventory.Api/bin/Debug/net10.0/Dewiride.Inventory.Api.dll

Build succeeded in 2.0s

Both projects build, the API last.

The Other Cause: A Missing using Directive​

The same error has a second common cause. With the reference in place but the using Dewiride.Inventory.Data; line missing from Program.cs, the build failed like this:

Output
Dewiride.Inventory.Api net10.0 failed with 1 error(s) (0.3s)
/Users/you/projects/dewiride-inventory-api/src/Dewiride.Inventory.Api/Program.cs(8,28): error CS0246: The type or namespace name 'DatabaseOptions' could not be found (are you missing a using directive or an assembly reference?)

Build failed with 1 error(s) in 1.0s

A terminal where dotnet build fails with error CS0246: The type or namespace name 'DatabaseOptions' could not be found, at Program.cs line 8, column 28, because the using directive for the library's namespace was removed.

Same error number, different clues: the name in quotes is the type, DatabaseOptions, and (8,28) points at the line that uses it. The library is referenced, but Program.cs doesn't import the namespace the type lives in. The fix is the using line, or the full name, Dewiride.Inventory.Data.DatabaseOptions.

How to tell them apart​

The name in quotesWhere it pointsMost likely causeFix
The first part of a namespace, such as 'Dewiride'A using lineThe project doesn't reference the assembly that defines the namespaceAdd the project reference or the package
A type, such as 'DatabaseOptions'The line that uses the typeThe type's namespace isn't imported, or the name is misspelledAdd the using, or fix the spelling

Check the spelling in both cases: a typo gives the same message.

Common Mistakes​

MistakeWhat happensDo this instead
Adding a using when the reference is missingThe same CS0246, now on the new lineAdd the reference first
Adding both projects to the solution and expecting that to be enoughCS0246: a solution groups projects, it doesn't connect themAdd a project reference
Referencing the right project, but the type is internalA different error: error CS0122: 'DatabaseOptions' is inaccessible due to its protection levelMake the type public if other projects should use it

Troubleshooting​

SymptomLikely causeFix
CS0246 names the first part of a namespace, such as 'Dewiride', on a using lineThe project doesn't reference the assembly that defines the namespaceAdd the project reference or the package
CS0246 names a type, on the line that uses itThe type's namespace isn't imported, or the name is misspelledAdd the using, or fix the spelling
CS0122: '…' is inaccessible due to its protection levelThe type is internal in the other projectMake it public if other projects should use it
CS0246 with both projects in the solutionA solution groups projects; it doesn't connect themAdd a project reference

FAQ​

Why does the error mention an "assembly reference"?​

Each project builds into an assembly, a .dll. A project reference or a package reference is how the compiler gets those assemblies. "Assembly reference" is the general term for both.

Can implicit usings cause or hide CS0246?​

They can hide it. .NET projects import some common namespaces automatically, such as System and System.Linq, so code that uses their types needs no using line. Your own library's namespaces are never imported automatically.

Conclusion​

CS0246 means the compiler can't find a name. If it can't find the first part of a namespace on a using line, the project is missing a reference to the assembly that defines it: add the project reference or the package. If it can't find a type, the project doesn't import the type's namespace, or the name is misspelled: add the using, or fix the name.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.