74

I've created a new Class Library in C# and want to use it in one of my other C# projects - how do I do this?

9 Answers 9

87

Add a reference to it in your project and a using clause at the top of the CS file where you want to use it.

Adding a reference:

  1. In Visual Studio, click Project, and then Add Reference.
  2. Click the Browse tab and locate the DLL you want to add a reference to.
    NOTE: Apparently using Browse is bad form if the DLL you want to use is in the same project. Instead, right-click the Project and then click Add Reference, then select the appropriate class from the Project tab:
    enter image description here
  3. Click OK.

Adding a using clause:

Add "using [namespace];" to the CS file where you want to reference your library. So, if the library you want to reference has a namespace called MyLibrary, add the following to the CS file:

using MyLibrary;
6
  • 13
    As long as the namespace is the same as the one you are using. If it is not, using is required. Aug 4, 2009 at 16:29
  • 1
    OR you could type namespace.objectname all the time. I find it easier to add using. Aug 4, 2009 at 16:30
  • 5
    Typically I only add a using if I'm going to be using something in that namespace frequently in that file. Otherwise I'll just fully qualify it to reduce "namespace clutter" in intellisense. Aug 4, 2009 at 16:31
  • 2
    Namespace clutter being "a great many items in the Intellisense drop-down?" That makes sense. Hadn't thought to try that. Aug 4, 2009 at 16:33
  • ur Note: solved a problem I had been looking into for 3 hours... THANKS Sep 11, 2013 at 16:23
7

In the Solution Explorer window, right click the project you want to use your class library from and click the 'Add Reference' menu item. Then if the class library is in the same solution file, go to the projects tab and select it; if it's not in the same tab, you can go to the Browse tab and find it that way.

Then you can use anything in that assembly.

1
  • 4
    When referencing the output of another project within the same solution, the quickest way to piss off your team members and create bugs is to BROWSE to the output of this project when adding a reference to it. Its essential that you use project references whenever possible. +1.
    – user1228
    Aug 4, 2009 at 16:15
3

I'm not certain why everyone is claiming that you need a using statement at the top of your file, as this is entirely unnecessary.

Right-click on the "References" folder in your project and select "Add Reference". If your new class library is a project in the same solution, select the "Project" tab and pick the project. If the new library is NOT in the same solution, click the "Browse" tab and find the .dll for your new project.

1
  • 1
    I have added a Class Library it is working fine in the local, but when I create MSI package then it's showing: Could not load file or assemblies, or one of its dependencies Mar 30, 2019 at 14:52
3

Right Click on Project--> Add--> New Project-->click on Class Library.

Now your class library is created as class1.cs.

Right Click on References (of your program/console app).

-->Add Reference-->classLibrary1 (whatever you named). Now mention "using ClassLibrary1" in your program/console app.

Now, you can easily call the method/property in your console app.

2

Here is a good article on creating and adding a class library. Even shows how to create Methods through the method wizard and how to use it in the application

2
  • I think your link above is the best answer. 1. Build your Class Library project. The DLL you need to reference will be inside the bin\Debug or bin\Release folder of the Class Library project. 2. In your other project, add a reference to the above mentioned DLL. 3. Use that DLL as explained in the post here.
    – nam
    Aug 7, 2017 at 21:17
  • 1
    I have added a Class Library it is working fine in the local, but when I create MSI package then it's showing: Could not load file or assemblies, or one of its dependencies Mar 30, 2019 at 14:52
1

Add it as a reference.

References > Add Reference > Browse for your DLL.

You will then need to add a using statement to the top of your code.

1

You need to add a reference to your class library from your project. Right click on the references folder and click add reference. You can either browse for the DLL or, if your class libaray is a project in your solution you can add a project reference.

1
  1. Add a reference to your library
  2. Import the namespace
  3. Consume the types in your library
1
  • I have added a Class Library it is working fine in the local, but when I create MSI package then it's showing: Could not load file or assemblies, or one of its dependencies Mar 30, 2019 at 14:52
1

There are necessary steps that are missing in the above answers to work for all levels of devs:

  1. compile your class library project
  2. the dll file will be available in the bin folder
  3. in another project, right click ProjectName and select "Add" => "Existing Item"
  4. Browser to the bin folder of the class library project and select the dll file (3 & 4 steps are important if you plan to ship your app to other machines)
  5. as others mentioned, add reference to the dll file you "just" added to your project
  6. as @Adam mentioned, just call the library name from anywhere in your program, you do not need a using statement
2
  • 2
    Yours is a better response. Here is a simple example of the steps you described.
    – nam
    Nov 3, 2015 at 2:35
  • @nam, this is an excellent article with an awesome graphi! I think the only missing part is copying the dll file to the target project because once you deploy the app to another machine, the dll file will be missing.
    – usefulBee
    Nov 3, 2015 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.