Breakpoints cannot be hit when debugging C# in Visual Studio

When this happens, there can be a few reasons

-You attached to the wrong process.

-The build profile was changed to Release instead of Debug, thus no symbols are generated for debugging.

-You opened multiple instances of Visual Studio or programs that lock the PDB file, so when you make a new build the PDB file fails to be regenerated to match the new DLL.

-There are assemblies with classes with same name in the same namespace (for example DI may be configured to load different assemblies depending on some configuration). Visual Studio load the debug file for the wrong assembly when you are debugging some code. VS may assume that there is only one implementation of a a class in same namespace.

Steps to resolve

Make sure you attach to the correct process.

Make sure the build profile is Debug.

Clean and rebuild the solution.

If there are multiple assemblies with classes with the same name, try deleting the one that is not in use and then rebuild. Best option is to avoid classes with same name in same namespace. VS will be confused.

Last step, log out and log in again, or restart the computer to kill the processes that are locking the PDB. It could even be some Visual Studio bug.