I am wondering how I can use a third party library in C#? For instance, I am coming from the python world, in which it is simple to add a third party module with a simple import statement. How can I add third party dll or libraries with C#?
-
If you have a managed dll ready see https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23%20add%20reference. There are other steps if you want to add a non-managed dll (like activex control). Also see NuGet and Visual Studio for existing libraries that are publicly available.. – Igor Aug 01 '16 at 15:08
-
It's called "Add Reference", and the line of code is `using Namespace.SubNamespace;`. See: https://msdn.microsoft.com/en-us/library/wkze6zky.aspx – Dan Bechard Aug 01 '16 at 15:10
2 Answers
In Visual Studio, right click on the references folder under your project in Solution Explorer and click "Add reference". Then add whatever dll you want and click OK. After that add one or more using
statements at the top for the corresponding library namespaces.
To add another project to references, it's similar to Python. Again in Solution Explorer, right click on your solution and then "Add" then "Existing Project". After adding the existing project you'll see it in Solution Explorer. You can add a using
statement at the top with the name of the relevant namespaces from the project you just referenced.

- 137
- 1
- 10
-
3Not just "whatever the dll name is" - the `using` statement takes a *namespace*, not an *assembly name*. They *can* be the same, but no guarantee – Mathias R. Jessen Aug 01 '16 at 15:09
-
Every time I have seen it they are the same. Apologies for the confusion. – Kooky_Lukey Aug 01 '16 at 15:13
-
2You probably have actually :) The `System` namespace is partially contained (along with 40-70 other namespaces, depending on version and edition of .NET) in an assembly called `mscorlib.dll` - not a single of the contained namespaces are named `mscorlib` :) – Mathias R. Jessen Aug 01 '16 at 15:22
-
Visual studio has a great tool called NuGet, which is the easiest way to add, remove and update 3-rd party libraries in your project. Take a look at this tutorial for more info.

- 1,433
- 9
- 18
-
3This assumes the library actually has a NuGet package. Is there a way to add external DLLs via NuGet if they are not packaged for NuGet? – Dan Bechard Aug 01 '16 at 15:12
-
No, as much as I know you can't. NuGet works with packages which contain references to external libraries. So in that case you actually have to manually add references to external DLL. – Alex Aug 01 '16 at 15:19
-
@Dan That does not make any sense to me. If the library does not have a NuGet package, you can add it directly, I don't understand why would you want to use NuGet for that. – svick Aug 01 '16 at 18:45
-
Here the advantages of using NuGet are clearly explained: http://stackoverflow.com/a/13005280/6356434 http://stackoverflow.com/a/9788469/6356434 – Alex Aug 02 '16 at 06:50
-
@svick That was exactly my point. This answer does not cover the case where a NuGet package doesn't exist. Which, honestly, is probably the majority of cases (depending on your environment, obviously). – Dan Bechard Aug 02 '16 at 17:52
-
@Dan The other answer already explains that. I think that answers that are applicable only sometimes are fine. – svick Aug 02 '16 at 19:26
-
@Dan I disagree with that. NuGet gallery has more than 60.000 packages and I personally have always found what I needed there. And what about other package managers like bower, composer and even apt-get? Are those useless too because you *might* not find a package you need there? – Alex Aug 03 '16 at 06:55
-
@Alex You're putting words in my mouth.. when did I say NuGet was "useless"? 60,000 packages is *significantly* less than 50% of existing C# libraries, a.k.a. less than the majority of cases. For instance, very few if any commercial libraries use NuGet, which are a vital part of the .NET ecosystem. A full answer would include how to import non-NuGet packages as well, as other, better answers here have done. Your answer is useful, but does not cover the majority of cases. – Dan Bechard Aug 03 '16 at 12:36
-
1@DanBechard Thanks for your response. Wish this was spoken more. I'm not sure why Alex & svick didn't understand why this would be helpful. But I appreciate it. 6 years later :) lol – justkeithcarr Jun 13 '22 at 14:33