.Net Core – Compilation Error – CS0101 – “The namespace already contains a definition” // “Type already defines a member with the same parameter types”

Background

Attempting to build a .Net core application, but ran into a blocking error.

Request

So to compile a .Net Core application, from the command line, I will simply navigate to the solution folder and issue a “dotnet build” command.

 

Error

Error Messages

Textual

  1. error CS0101: The namespace [project] already contains a definition
  2. error CS0111: Type [class-name] already defines a member called [member-function] with the same parameter types

Image

TroubleShooting

Find matching text

Findstr

Syntax


findstr [pattern] [filename]

Sample


findstr /P /s "ConsoleApp1" *.cs | findstr /v obj

Output

Output – Textual


>findstr /P /s "ConsoleApp1" *.cs | findstr /v obj
deprecate\20200710.0437PM\Program.cs:namespace ConsoleApp1
Program.cs:namespace ConsoleApp1

Output – Image

Explanation

There are two files that have ConsoleApp1. Our actual file and a backup/deprecated file.

Remediation

Project File ( csproj )

Please edit your project file ( .csproj) and exclude the unneeded files.

Outline

  1. Add Section :- ItemGroup
  2. Add Items
    • Add Item:- Compile
    • Add Item:- EmbeddedResource
    • Add Item:- None

Original


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>

    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>

  </PropertyGroup>

</Project>

Revised



<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>

    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>

  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="deprecate\**" />
    <EmbeddedResource Remove="deprecate\**" />
    <None Remove="deprecate\**" />
  </ItemGroup>
  

</Project>

 

Reference

  1. Microsoft
    • Docs > .NET > .NET Core > guide > Tools
      • dotnet command
        Link