A Filenaming convention for C#

The Problem

While experimenting for my series of articles on building a development environment using Visual Studio 2003, I came across the following:

Let’s say you have a software component which implements a particular concern of your application, and this component has a class named MyClass. Thus, in Visual Studio we have a project of type “Class Library” which is contained in a solution and inside this project we have a file MyClass.cs which contains the code for our class. You compile a debug version of this library which gives you a DLL and a PDB file (program debug database).

Next you have your application which also has a class MyClass, but in a different namespace, however also in a file called MyClass.cs. Your application uses the above library, but it references the library DLL itself and NOT the Visual Studio project file. For this you copy the DLL and corresponding pdb file to the debug folder of your application. Also, the source code of the library is no longer available at the location on your harddrive where it was when the library dll got build.

Now, when you start debugging your application and want to step into the code of the MyClass of your library, Visual Studio jumps around in the MyClass.cs file of your application.

What is happening

It is clear that Visual Studio is mixing up the sourcefiles of your library with the ones of your application:
Because the source code is no longer available at the original location, Visual Studio can not go there to look for it. But because the sourcefile in your application has the same name as the one in your library Visual Studio assumes this is the source file of your library and starts jumping around in your applications file as if it where your libraries file.

A possible solutions

Naming conventions
The solution is simple, keep the names of your sourcefiles unique. The easiest way to do this is simply by prepending the namespace to the name of your files.
This code like this:

namespace ClassLibrary
public class MyClass
// Rest of class ommited

ends up in a file named ClassLibrary.MyClass.cs

Your environment
I’ve tried the same in Visual Studio 2005 and found no problem there. So you could switch to this IDE. But this will not be possible to everyone of course.


Leave a Reply

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