I have a .cs file full of C# code I keep reusing in multiple projects.
Right now I’m including it in these projects by copying and pasting the code into a new file in each project directory. This is the wrong way to do it.
What’s the right way to do it?
The right way should:
- keep the common code in only one place on my computer
- and keep the link fresh — so when the common code changes, each project is aware of it, the next time I recompile that project.
Randomly guessing, I’d expect some sort of directive like using mycode = C:\common\mycode.cs, but I’m sure that’s not the .NET way of doing things.
(I’m using C# 2010, .NET 4.0, and only compiling this code locally on one computer.)
Create a Class Library, add the file, build the project, and reference the DLL created from the build. Add the using statement to each file that will reference it. Also if it errors and the DLL is in the Project you and Right Click on the object -> Resolve and it will add the using for you.