Take the 2-minute tour ×

I'm kinda new to c# and I'm trying to figure out a good solution for keeping common code somewhere that is used by several projects.

First I tried keeping it in a .cs file and add it to the projects that needs the code, but this COPIES the file meaning if I do any changes to the common code it will not be reflected in the various projects using this code.

Then I tried creating a class library instead and reference the DLL within the projects using the code. First of all I can't get this to work even though I am following the tutorials, Even when I reference the DLL I still cannot use it somehow, but even if this worked it still needs an extra DLL to be bundled with my app for some common code and that is not what I want.

Is there any way to keep common code that is REFERENCED without needing to ship a DLL w

share|improve this question

5 Answers 5

up vote 7 down vote accepted

The best solution is to have a common library that you reference in each of your projects. However you say you don't want to do that as it requires bundling additional dlls, is that really that big a issue? You also mention that you can't get that to work, post the details of errors etc and we will be able to help.

Another option, assuming you are using Visual Studio is to link the common file into your project rather than add it. This will keep the common file in its current location and not move it into your project directory.

To do this, go to the add existing file dialog but on the 'Add' button click the down arrow and select 'Link' instead.

As mentioned above using a common library is the best solution so if bundling this is a unsurmountable issue you could look into using ilmerge. This will merge all your dlls/exes into a single file that you can then deploy.

share|improve this answer
Great, I've never noticed that little arrow to add as a link instead, that was exactly what I needed, thanks! –  Kristian Erik Tigersjäl Dec 3 '10 at 13:22
As to why adding a class library fails I have no idea, I've created the class library and compiled it and it looks fine. I reference it in the solution using it but when I try adding the using code for it or even try to access it it's as if it's not been referenced at all, and I cannot see anything wrong with the library code either strangely enough. –  Kristian Erik Tigersjäl Dec 3 '10 at 13:23
Removed actual code, but this is what it looks like: using System; namespace textfile { class textfiletools { public static bool readfile(string filename, out string[] dest) { } public static bool writefile(string filename, string[] data) { } } } –  Kristian Erik Tigersjäl Dec 3 '10 at 13:24
Its difficult to be certain but this sounds like either a namespace issue or maybe all the classes in your common library are marked as private or internal. EDIT: After seeing your last comment, if you make the textfiletools class public you should be able to use it (i.e. public class textfiletools). Without an access modifier the class is assumed to be private so your referencing library won't be able to see that class or the methods within it, even though these are marked a public. –  MrEyes Dec 3 '10 at 13:24
all classes marked as public apart from the "parent class" but I tried marking that public too with no effect. this one really puzzles me I'm sure it's just a stupid mistake I cannot see which makes it bother me even more. ;) EDIT: NOT parent class, I meant the methods are all public but the class is not and changing it didn't make any difference. –  Kristian Erik Tigersjäl Dec 3 '10 at 13:28

You can add a link to a common file:

  1. Right click on the project
  2. Select 'Add' => 'Existing Item...'
  3. Find the .cs file you want to share
  4. Click the drop down on the Add box
  5. Choose, Add as Link.

Hope this helps!

share|improve this answer
Assuming you're using Visual Studio!!! –  Nick Dec 3 '10 at 13:07

You were almost there. You can add your existing file, but note that you can expand the "add" button and choose to link to it instead.

share|improve this answer

You can add a linked file to a project - this does not create a copy but simply references it.

See the "Adding an Existing Item as a Link" section in the linked page:

To create a link to an existing item:

  1. In Solution Explorer, select the target project.
  2. On the Project menu, select Add Existing Item.
  3. In the Add Existing Item dialog box, locate and select the project item you want to link.
share|improve this answer

In visual studio you can add a link to an existing item:

In the solution explorer, right click and choose Add -> Existing Item... Browse to the file and instead of clicking Add, choose Add As Link.

But I would advise to use a separate project for common code.

share|improve this answer

Your Answer


