How do I keep common code shared between projects in c#?

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

Upvotes: 9

Views: 8451

Answers (5)

Nick
Nick

Reputation: 25799

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.

Upvotes: 3

Samuel
Samuel

Reputation: 1374

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.

Upvotes: 0

MrEyes
MrEyes

Reputation: 13690

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.

Upvotes: 12

Oded
Oded

Reputation: 499002

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.
  4. From the Open button drop-down list, select Add As Link.

Upvotes: 0

user27414
user27414

Reputation:

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.

Upvotes: 1

Related Questions