CS0101: The namespace ‘namespace_name’ already contains a definition for ‘class_name’. Visual Studio Error CS0101.


Recently I received the error: CS0101: The namespace ‘namespace_name’ already contains a definition for ‘class_name’ for one of my web applications.  There may be a number of reasons why this occurs but before I share why this happened to me, here is the background on the problem.

I had created a file named msAccess.cs.  Visual Studio assigned the name “msAccess” to my namespace.  I gave my class in the name MsAccessConnection.  Initially when I wrote the web application I thought it was going to be a very straightforward app.  However, like most things it grew and evolved.  I saw many things wrong with my code and decided to restructure my msAccess.cs file itself.  But for the sake of maintaining preserving the functioning copy in case anything went bad, I renamed my msAccess.cs file to msAccess_old.cs.  Then when I recreated my new one, I gave it the original name msAccess.cs.

When Visual studio found both cs files in the directory, it read the namespaces in both.  Eventhough one was set to compile and the other was excluded from the project, Visual Studio still read the excluded .cs file (i.e. msAccess_old.cs).  Therefore I received the error: CS0101: The namespace ‘msAccess’ already contains a definition for ‘MsAccessConnection’.

Little bugs like this often frustrate me, especially when the solution is so simple and is overlooked.  Therefore, if you turn to Google or any other search engine as I often do, I hope this post is relevant and helped you.

Oh, as a solution, I renamed my msAccess_old.cs file to msAccess_old.txt.  I just changed the file extension and that did the trick.

6 Comments

Filed under Computer - Technical

6 responses to “CS0101: The namespace ‘namespace_name’ already contains a definition for ‘class_name’. Visual Studio Error CS0101.

  1. Cyber

    Damm… ” Visual Studio assigned the name “msAccess” to my namespace. ” . That was exactly my problem…

    Thx for the post! It helped me a lot!😉

  2. Shahejad

    I have same issue after upgrading my SP 2007 solution SP 2010.

    What I did to resolve this issue is remove all references and than rebuilt each project and added reference to other project and everything works fine.

  3. Lenae

    This was exactly my problem! You saved me so much trouble. I thought my new file was broken. I was getting ready to start tearing it apart but I thought to Google the error message instead. PHEW! Thanks for sharing!

  4. Pingback: Fix Sms Error 98 Error Class 2 Windows XP, Vista, 7, 8 [Solved]

  5. This saved me in unreal engine while compiling a project. I had duplicate files, and was able to locate and rename one of ’em. Happiness is once again restored!

Comment on this

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s