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.